Moving cube using joystick.js script in c# for mobile devices in unity3d

  1. I assume that you already have setup of Unity3d and have the basic knowledge of unity editor.
  2. Create new project in Unity3d form File->NewProject. Save it as “JoyStickExample”
  3. Save scene as “CubeMovemetScene” from File->Save Scene.
  4. Add a plane, cube and directional light on the scene from GameObjet->create Other-> plane/Cube/Directional Light. So that the scene will be visible, other wise it will be a blackout.
  5. Adjust the camera view to see the full plane as like following image.                                             UnityMCJMFig1
  6. Import Standard Assets for mobile from Assets->Import Packages-> Standard Assets (Mobile).
  7. It will open a window which shows the packages that it will include in project. Clicking on the “Import” button unity will import the selected packages into the assets folder.                                                                                                                                                                   UnityMCJMFig2
  8. Create folder “Standard Assets” in Assets folder and  add the dual joyStick prefab to Hierarchy. As we have added the “Standard Assets (Mobile)” which includes joystick prefabs. So search in project directory for joy stick, it will show the “Dual Joystics” prefab.                                                                                                                                                                                     UnityMCJMFig3
  9. Drag the “Dual Joystics” to Hierarchy and set the tags to “LeftJoystick” prefab as “LeftJoystick” tag and “RightJoystick” prefab as “RightJoystick” tag from Inspector.
    1. In Inspector view,  for “Tag-> Add Tag”. Add tag “LeftJoystick” and “RightJoystick” .                                                                           UnityMCJMFig4
    2. Select “LeftJoystick” from Hierarchy and set tag “LeftJoystick” in “Tag” option. Do the same for “RightJoystick” as shown in following image.                                                                                                   UnityMCJMFig5.1UnityMCJMFig5.2
  10. Create “GameScript” folder in Assets folder and add C# script “CubeMovement” from Assets->Create->C# Script.                                                                                                                             UnityMCJMFig6
  11. Select the “Cube” in Hierarchy. It will show it’s property in Inspector Window. And  from Inspector window of Cube, press “Add Component” and select “CubeMovement” script.                                                                                                         UnityMCJMFig7
  12. Double click on “CubeMovement” script it will open in “MonoDevlop” editor as like following image. and add following code in it.
public Joystick joystickLeft;
public Joystick joystickRight;

UnityMCJMFig8

13.  Try to build the code.

14.  Ohhh NO!!! It gives me error as shown in following image… 🙁

UnityMCJMFig9

15.  So go back to unity editor and check Joystick.js file present or not in Project directory.  And yes it is present, as we have added the “Standard Assets (Mobile)”. Then where is the problem?

UnityMCJMFig10

16.  Don’t worry here is the problem

  1. Error is because it cannot able to find the “Joystick” class at run time, because of unity compiler compiling sequence.
  2. Unity compiler load first “CubeMovement.cs” script and then “Joystick.js” file.For reference you can also refer the unity link:

              http://docs.unity3d.com/Documentation/Manual/ScriptCompileOrderFolders.html

17.  To solve this, we need to do is move the “Joystick.js” from Assembly-UnityScript ->Standard Assets (Mobile)->Script(as like following image)  to “Standard Assets” folder as it compiles first.

UnityMCJMFig11

18.  So for this first I will import package “Script” from Assets->Import Packages-> Script. It will open a window which shows the packages that it includes. So press “Import”. I need this because it create me the “Standard Assets” folder under “Assembly-UnityScript”.                  UnityMCJMFig12

19.  Open the “MonoDevlop” editor it will show the “Standard Assets” folder under “Assembly-UnityScript-firstPass”.                                                                                                                                     UnityMCJMFig13

20.  DRAG the “Joystick.js” file to “Standard Assets” folder.                                                                         UnityMCJMFig14

21.  Again “build All” the solution. And WOW no error for Joystick variable in “CubeMovement” script.

UnityMCJMFig15

22.  Replace all code in your “CubeMovement.cs” file with the following.

 

 
using UnityEngine;
using System.Collections;

public class CubeMovement : MonoBehaviour {

	public Joystick joyStickLeft = null;
	public Joystick joyStickRight = null;

	public float rotationSpeed = 0.5f;

	// Use this for initialization
	void Start () {

		// this is to set the joysticks
		if(joyStickLeft ==null){
			GameObject objjoyStickLeft = GameObject.FindGameObjectWithTag("LeftJoystick") as GameObject;
			joyStickLeft = objjoyStickLeft.GetComponent();
		}

		if(joyStickRight==null){
			GameObject objjoyStickRight = GameObject.FindGameObjectWithTag("RightJoystick") as GameObject;
			joyStickRight = objjoyStickRight.GetComponent();
		}

	}

	// Update is called once per frame
	void FixedUpdate () {

		Vector3 position = this.transform.position;

		float xMovement = 0;
		float yMovement = 0;
		 // this is to move the cube
		if(joyStickLeft.position.x<0) { 			xMovement = xMovement - 0.01f; 		} 		if(joyStickLeft.position.x>0) {
			xMovement = xMovement + 0.01f;
		}

		if(joyStickLeft.position.y<0) { 			yMovement = yMovement - 0.01f; 		} 		if(joyStickLeft.position.y>0) {
			yMovement = yMovement + 0.01f;

		}

		position.x = position.x+ xMovement;
		position.z = position.z + yMovement;
		this.transform.position = position;

		// to rotate the cube
		float rotatePos = joyStickInput(joyStickRight);
		transform.Rotate(0, rotatePos * rotationSpeed, 0);

	}

	float joyStickInput (Joystick jstick) {
		Vector2 absJoyPos =new Vector2 (Mathf.Abs(jstick.position.x),
		                         Mathf.Abs(jstick.position.y));
		int xDirection = (jstick.position.x > 0) ? 1 : -1;
		int yDirection = (jstick.position.y > 0) ? 1 : -1;
		return ( ( absJoyPos.x > absJoyPos.y) ? absJoyPos.x * xDirection : absJoyPos.y * yDirection);
	}

}

23.  Al the set up completed. So run it on mobile device.