"Turn" simple virtual joystick Control movement (NGUI)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.