Ugui Implement Joystick

Source: Internet
Author: User

Cubes are like characters. You can use the joystick in the lower right corner to control character movement

Look at the structure chart

Image1: It's the outer circle.

Image2: It's a blue circle.

Create JoyStickScript.cs code to mount on Image2. Control cube movement is not implemented here. needs to be improved

1 usingUnityengine;2 usingSystem.Collections;3 usingUnityengine.ui;4 usingUnityengine.eventsystems;5 6 /// <summary>7 ///rocker Lever8 /// </summary>9  Public classJoystickscript:monobehaviour, Ibegindraghandler, Idraghandler, IenddraghandlerTen { One  AGameobject Player;//Moving Objects -  -Vector3 originposition;//Start coordinates the Vector3 newposition; -      Public floatRadius = +;//Maximum radius of scrolling -     floatHorizontal; -     floatVertical; +     floatSpeed ; -     voidAwake () +     { APlayer = Gameobject.findwithtag ("Player"); at     } -  -     //Use this for initialization -     voidStart () -     { -         //originposition = transform.position;//Record Origin location in     } -  to     //Update is called once per frame +     voidUpdate () -     { the         //Float horizontal = Input.getaxis ("horizontal"); *         //float veritcal = (TRANSFORM.POSITION.Y-ORIGINPOSITION.Y)/Radius; $         //vertical = (TRANSFORM.POSITION.Y-ORIGINPOSITION.Y)/Radius;Panax Notoginseng         //horizontal = (transform.position.x-originposition.x)/Radius; -         //Player.transform.Translate (horizontal, vertical, 0); the         //Debug.Log (vertical); +  A     } the  +      Public voidOnenddrag (pointereventdata eventData) -     { $Transform.position = originposition;//homing to Origin $     } -  - Vector3 dir; the      Public voidOndrag (pointereventdata eventData) -     {Wuyi  theDir = (Input.mouseposition-originposition). normalized; -  WuTransform.position = input.mouseposition;//set Current joystick position -  About         //when it's over the outside Circle $         if(Vector3.distance (Input.mouseposition, originposition) >Radius) -transform.position = originposition + dir *Radius; -  -         //vertical = (TRANSFORM.POSITION.Y-ORIGINPOSITION.Y)/Radius; A         //horizontal = (transform.position.x-originposition.x)/Radius; +Print (Originposition + dir *Radius); thePlayer.transform.position = dir *Time.deltatime; -     } $  the      Public voidOnbegindrag (pointereventdata eventData) the     { theOriginposition = transform.position;//Record Origin location the     } -}

Ugui Implement Joystick

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.