[Unity3d] Unity3d game development using Easytouch virtual joystick to control character movement

Source: Internet
Author: User
Tags bind touch

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. As shown in figure:


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 () {Easyjoystick.on_joystickmov when joystick is available
        E + = 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 joystick is destroyed;
    Easyjoystick.on_joystickmoveend-= Onjoystickmoveend; }//When the joystick is in a stopped state, the role enters standby void Onjoystickmoveend (Movingjoystick move) {if (Move.joystickname = = "Easyjoys Tick ") {animation.
        Crossfade ("idle"); }}//When the joystick is moving, the character starts running void Onjoystickmove (Movingjoystick move) {if (Move.joystickname! = "Easyjoy
        Stick ") {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 joystick
		mjoystick=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);
			Move the player's position (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 author: Qin Yuanpei, this article source: http://blog.csdn.net/qinyuanpei/article/details/26204177


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.