[Unity3D] Unity3D game development-use EasyTouch virtual joystick to control character Movement

Source: Internet
Author: User

Hello, everyone. Welcome to my blog. I am Qin Yuanpei and my blog address is blog.csdn.net/qinyuanpei. Today, let's learn how to use the EasyTouch virtual joystick in Unity3D to control the movement of characters. Although Unity3D has a built-in Joystick component (actually a GUITexture and a Js script file ), however, in actual use, the blogger finds that the built-in Joystick cannot adapt to the screen size. Therefore, the blogger recommends EasyTouch plug-in here, we can quickly integrate the virtual joystick feature in the application and customize the joystick's appearance by changing the texture, this plug-in is the best plug-in the touch interaction feature. First, import the EasyTouch resource package, and then add a virtual joystick through Hedgehog Team-> Extension-> Adding a new Joystick. At this time, the EasyTouch and Easy joystick objects are generated in the hierarchical view.


In this case, we can see the default virtual joystick appearance of EasyTouch in the game scenario:


Next, let's take a look at the EasyJoystick attributes:

Well, if you have no problems with these attributes, we can start to write the script:

Using UnityEngine; using System. collections; public class MoveController: MonoBehaviour {// register the event void OnEnable () {EasyJoystick when the joystick is available. on_JoystickMove + = OnJoystickMove; EasyJoystick. on_JoystickMoveEnd + = OnJoystickMoveEnd;} // remove the event void OnDisable () {EasyJoystick when the joystick is unavailable. on_JoystickMove-= OnJoystickMove; EasyJoystick. on_JoystickMoveEnd-= OnJoystickMoveEnd;} // removes the event void OnDestroy () {EasyJoystic when the joystick is destroyed. K. on_JoystickMove-= OnJoystickMove; EasyJoystick. on_JoystickMoveEnd-= OnJoystickMoveEnd;} // when the joystick is stopped, the role enters the standby status void OnJoystickMoveEnd (MovingJoystick move) {if (move. joystickName = "EasyJoystick") {animation. crossFade ("idle") ;}// when the joystick is moved, the role starts to run void OnJoystickMove (MovingJoystick move) {if (move. joystickName! = "EasyJoystick") {return;} // obtain the joystick offset float joyPositionX = move. joystickAxis. x; float joyPositionY = move. joystickAxis. y; if (joyPositionY! = 0 | joyPositionX! = 0) {// set the role orientation (orientation to the current coordinate + joystick offset) transform. lookAt (new Vector3 (transform. position. x + joyPositionX, transform. position. y, transform. position. z + joyPositionY); // The Position of the mobile player (moving by orientation) transform. translate (Vector3.forward * Time. deltaTime * 7.5F); // play the animation. crossFade ("Run ");}}}
We bind this script to our character model and run the program:

Here, let's talk about how to use the Joystick provided by Unity3D to implement character Movement Control. First, we need to import the Standard Assets (Mobile) resource package. In this resource package, we can find two useful files: joystick. js and JoystickThumb. psd, which are the script components and appearance textures of the virtual joystick. We manually create a GUITexture and adjust it to the appropriate position (the tragedy begins here ), assign this texture to GUITexture and bind it to Joystick. js script. The blogger is used to C # and does not use many Js scripts. It is said that Js scripts in Unity need to be placed in any file in Standard Assets, Pro Standard Assets, and Plugins for normal use, therefore, many of my friends reported that the built-in Joystick of Unity could not find the class, which is the reason. Here, bloggers use C # scripts rewritten by foreign friends based on official scripts. We compile a C # script to respond to the virtual joystick:

Using UnityEngine; using System. collections; public class JoystickController: MonoBehaviour {private Joystick mJoystick; private float mSpeed = 7.5F; void Start () {// get JoystickmJoystick = GameObject. find ("UnityJoystick "). getComponent <Joystick> ();} void Update () {// obtain the response value float mX = mJoystick. position. x; float mY = mJoystick. position. y; // move the role if (mX! = 0 | mY! = 0) {Vector3 mPos = new Vector3 (transform. position. x + mX, transform. position. y, transform. position. z + mY); transform. lookAt (mPos); // The Position of the mobile player (moving by orientation) transform. translate (Vector3.forward * Time. deltaTime * 5F); // play the animation. play ("Run");} else {animation. play ("idle ");}}}
The official virtual joystick component cannot be tested on a computer. You need to install it on your mobile phone. Let's take a look at the actual results:


The EasyTouch plug-in can run perfectly on mobile phones. The official Joystick does not reflect it. I don't know why. If I have time, continue to study it.


References:

1. Unity mobile game Path <5> Unity built-in plug-in implementation of virtual joystick

2. Unity mobile game road <6> Easy Touch for game joystick 3 tutorial


Daily proverbs: Don't look around when you are determined to take the road, and never linger on the beautiful scenery.




If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei
Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/26204177


 

Related Article

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.