Http://www.cnblogs.com/zhangbaochong/p/4928688.html
First, create a virtual joystick map with Ngui
First create a sprite as the background called joystick and add a boxcollider, and then create a sprite child as the middle of the virtual Joystick button, called button
Second, the X, Y offset value is obtained through the virtual joystick.
1 using Unityengine; 2 using System.Collections; 3 4 public class Joystick:monobehaviour 5 {6 7 private bool ispress = false; 8 private Transform button; 9 1 0//x, y offset from virtual joystick-1 to 1 public static float h = 0;12 public static float v = 0;13 void Awake () 14 { The button = transform. Findchild ("button"),}17 void onpress (bool ispress), {this.ispress = ispress;20 if (!isp ress) {button.localposition = vector2.zero;23 h = 0;24 v = 0;25} }27-void Update () (ispress) {Vector2 Touchpos = Uicamera.laste Ventposition-new Vector2 (a): Distance float = vector2.distance (Vector2.zero, touchpos); 34 if (Distance > 73)//Virtual joystick button cannot exceed radius of {touchpos = touchpos.normalized * 73;37 } button.localposition = touchpos;39 40 H = touchpos.x/73;41 v = touchpos.y/73;42}43}44}
Third, the character controller has been added by moving the protagonist through the migration control
1 using Unityengine; 2 using System.Collections; 3 4 public class Playermove:monobehaviour 5 {6 private Charactercontroller cc; 7 public float speed = 3 F 8 9 void Awake () {one cc = getcomponent<charactercontroller> (); }13 // Update is called once per frame15 void Update () + //keyboard control (Float h = input.getaxis ("horizontal") ): Float v = input.getaxis ("Vertical"); //Virtual joystick control if (JoyStick.h! = 0 | | Joystick.v! = 0) (+ h = joystick.h;25 v = joystick.v;26 }27 if (mathf.abs (h) > 0.1f | | M ATHF. Abs (v) > 0.1f) { Vector3 targetDir = new Vector3 (h, 0, v); transform. LookAt (TargetDir + transform.position), cc. Simplemove (TargetDir * speed), }34 }37}
"Go" simple virtual joystick Control movement (NGUI)