Pure Unity Touch JoyStick

Source: Internet
Author: User

Recently in the study of Unity3d, the research process encountered problems recorded as a backup, and later use of relevant knowledge can be used for reference.
Recently there is an idea, want to do a small practiced hand game, while doing learning Unity3d. Because it is 3D game, controlled by joystick, so first study here. Designed to find a pure way of writing, convenient for later expansion.

The current Unity version is 5.3.0, which studies the Easytouch 4.2.2 under fire. Summed up found that the most applied now seems to be Easytouch v2 or v3, to 4 is the code is interrelated, not good extraction.

The following is a simple joystick developed with Ugui, providing joystick movevector (sliding direction), the interface is relatively clear, the following start.

The following structures are made by Ugui, respectively, the Basemap (backgroundimage) and the rocker (Joystickimage).

Adjust the position on the interface to the place you like, and adjust the color as follows.

The next way to write joystick.

1 usingUnityengine;2 usingSystem.Collections;3 usingUnityengine.ui;4 usingUnityengine.eventsystems;5 6  Public classJoystick:monobehaviour, Idraghandler, Ipointeruphandler, Ipointerdownhandler//Inherit these interfaces, detect drag, click, etc.7 {8     PrivateImage bgimg;9     PrivateImage joystickimg;Ten     PrivateVector3 Inputvector; One  A     Private voidStart () -     { -Bgimg = getcomponent<image>(); theJoystickimg = transform. Getchild (0). Getcomponent<image>(); -     } -  -      Public Virtual voidOndrag (pointereventdata eventData) +     { - Vector2 Pos; +         if(Recttransformutility.screenpointtolocalpointinrectangle (Bgimg.recttransform, eventdata.position,// Convert the current click position to the position relative to the joystick (joystickimg) AEventdata.presseventcamera, outPOS)) at         { -  -Pos.x = (Pos.x/bgimg.recttransform.sizedelta.x); -Pos.y = (POS.Y/bgimg.recttransform.sizedelta.y); -  -Inputvector =NewVector3 (Pos.x *2,0, Pos.y *2); inInputvector = (Inputvector.magnitude >1.0f) ?InputVector.normalized:inputVector; To perform some standardized processing, at which time the inputvector is a normalized vector of ( -1,1) -  to             //Move Joystick IMG +JoystickImg.rectTransform.anchoredPosition =NewVector3 (//achieve click Position, joystick (joystick) movement. Where parameters can be self-adjustable -Inputvector.x * (bgimg.recttransform.sizedelta.x/2), theInputvector.z * (BGIMG.RECTTRANSFORM.SIZEDELTA.Y/2)); *  $ Debug.Log (inputvector);Panax Notoginseng         } -     } the  +      Public Virtual voidOnpointerup (Pointereventdata eventData)//When the finger is lifted, the joystick (joystick) is zeroed A     { theInputvector =Vector3.zero; +JoystickImg.rectTransform.anchoredPosition =Vector3.zero; -     } $  $      Public voidOnpointerdown (pointereventdata eventData) -     { - Ondrag (eventData); the     } - Wuyi      Public floathorizontal ()//returns x, providing an interface where Getaxis provides interfaces for cross-platform the     { -         if(Inputvector.x! =0) Wu             returninputvector.x; -         Else About             returnInput.getaxis ("Horizontal"); $     } -  -      Public floatVertical ()//return z, provide interface -     { A         if(Inputvector.z! =0) +             returnInputvector.z; the         Else -             returnInput.getaxis ("Vertical"); $     } the}

Drag the script onto the backgroundimage and save as prefab.

Next talk about how to use, to achieve the effect of the object in the plane motion.

First create a 100*100 cube site and create a sphere as a sprite.

Create the Ballmove script as follows:

1 usingUnityengine;2 usingSystem.Collections;3 4  Public classBallmove:monobehaviour5 {6      Public floatMovespeed =10.0f;7      PublicVector3 Movevector {Set;Get; }8      PublicJoystick Joystick;9 Ten     voidUpdate () One     { AMovevector =poolinput (); - Move (); -     } the  -     Private voidMove () -     { +Transform.position = transform.position + movevector*movespeed*Time.deltatime; -     } +  A     PrivateVector3 poolinput () at     { -Vector3 dir =Vector3.zero; -  -Dir.x =joystick. Horizontal (); -Dir.z =joystick. Vertical (); -  in         returndir; to     } +}

The script is very simple, not much to say. In short, from the joystick to take out the standardized vector, Movevector, how to deal with it can expand their own.
Drag the script onto the target object.

It's here today, just touching unity, something that you understand, and mistakes can be corrected.

Pure Unity Touch 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.