Hello everyone, welcome to my blog, I am Qin Yuanpei, my blog address is Blog.csdn.net/qinyuanpei. Today, let's learn how to use 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), bloggers actually use it to find that the built-in joystick doesn't fit the size of the screen. So bloggers here to recommend the use of Easytouch this plugin, through this plug-in, we can quickly integrate the virtual joystick function in the application, and can change the way the map to customize the appearance of the stick, so overall, this plug-in is in the touch of this part of the function of the best plug-in. First we import the Easytouch resource bundle, and then add a virtual joystick through hedgehog team->extension->adding a new joystick, which generates Easytouch and easy in the hierarchy view Joystick of two objects.
At this point, we can see the Easytouch default virtual joystick appearance in the game scene:
Next, let's explain the properties of Easyjoystick:
Well, if you have no problem with these attributes, then we can start writing the script:
Using unityengine;using System.collections;public class Movecontroller:monobehaviour {//Register event void Onenable () when joystick is available {Easyjoystick.on_joystickmove + = Onjoystickmove; Easyjoystick.on_joystickmoveend + = Onjoystickmoveend; }//when the joystick is not available remove event void Ondisable () {easyjoystick.on_joystickmove-= Onjoystickmove; Easyjoystick.on_joystickmoveend-= Onjoystickmoveend; }//Remove event void OnDestroy () {easyjoystick.on_joystickmove-= Onjoystickmove When the joystick is destroyed; Easyjoystick.on_joystickmoveend-= Onjoystickmoveend; }//when the joystick is in the stopped state, the role enters standby void Onjoystickmoveend (Movingjoystick move) {if (Move.joystickname = = "Easyjoystick") {animation. Crossfade ("idle"); }//the character starts running void Onjoystickmove (Movingjoystick move) {if (Move.joystickname! = "Easyjoystick") when the joystick is moved {return; }//Get joystick offset float Joypositionx = move.joystickaxis.x; float Joypositiony = MOVE.JOystickaxis.y; if (joypositiony! = 0 | | Joypositionx! = 0) {//sets the orientation of the role (toward the current coordinate + joystick offset) transform. LookAt (New Vector3 (transform.position.x + Joypositionx, TRANSFORM.POSITION.Y, Transform.position.z + joypositiony)); Move the player's position (moving toward position) transform. Translate (Vector3.forward * time.deltatime * 7.5F); Play run animation animation. Crossfade ("Run"); } }}
We bind this script to our character model and then run the program:
Here we'll take another look. Using Unity3d's joystick to achieve character movement control, we first need to import the standard Assets (MOBILE) Resource pack, In this resource bundle we can find two useful files: Joystick.js and JOYSTICKTHUMB.PSD, which are the script component and the appearance map of the virtual joystick, we manually create a guitexture and adjust it to the right place (the tragedy starts here) , assign the map to Guitexture, and then bind the Joystick.js script. Bloggers are accustomed to using C #, for JS scripts are not used much, it is said that in Unity JS script needs to be placed in the standard Assets, Pro Assets and plugins any one of the files in order to use, So many friends reflect the use of unity built-in joystick can not find the class, this is the reason. Bloggers here use a C # script rewritten by a foreign friend based on an official script. We write a C # script to respond to virtual sticks:
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 () { //Get joystick response value float mx=mjoystick.position.x;float my=mjoystick.position.y; Move 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 player (moving toward position) transform. Translate (Vector3.forward * time.deltatime * 5F);//play Run animation animation. Play ("Run");} Else{animation. Play ("Idle");}}}
The official offer of this virtual joystick component cannot be tested on a computer and needs to be installed on the phone to run, so let's take a look at the actual effect:
On the phone Easytouch plug-in can be perfectly run, the official provided joystick not reflect, at present do not know why, have time to continue to study ah.
Reference article :
1, Unity Hand Tour < five > virtual joystick Unity Built-in plug-in implementation
2. Unity Hand Tour < six > Game joystick Easy Touch 3 tutorial
daily motto: when determined to take a road, do not look around, scenery and beauty don't linger
Like my blog Please remember my name: Qin Yuanpei, my blog address is Blog.csdn.net/qinyuanpei
Reprint please indicate the source, this article Qin Yuanpei, this article source: http://blog.csdn.net/qinyuanpei/article/details/26204177