Welcome to Unity Learning,Unity Training,UnityEnterprise TrainingEducation Area, there are manyu3d Resources,u3d Training Video,u3d Tutorials,U3d Frequently Asked questions,u3d Project Source code, the "Dog Planing Learning Network" Unity's Ultimate Academy, dedicated to building the industryUnity3dTraining, learning the first brand.
Untiy3d Official joystick script can only complete a joystick, to achieve a double stick more laborious
Now I'm going to waste a whole night on the results of sharing to everyone
1. Dual joystick is used to control the first person controller, but the first person controller right JS and C # Some places can not be common, so first of all will be changed to C # script
Http://unity.gopedu.com/thread-9313-1-1.html
2. Create a Joystick.cs script, I also added a comment in the code to facilitate people to understand
3. Put the GUI stick texture into the script and associate the script with the first person controller
4. Modify the first person controller script
/**
* *joystick.cs
*/
Using Unityengine;
Using System.Collections;
Create an enumeration type to choose whether it is a left or right joystick on the entity that loads the script
public enum Joysticktype
{
Leftjoystick,
Rightjoystick
}
Script Joystick Class
public class Joystick:monobehaviour
{
Public Joysticktype Joysticktype; Rocker type, left joystick or right joystick
Private Vector2 Centerpos; Center point position of the joystick, screen coordinates
Public Guitexture Centerball; Ball type Rocker
public float Joystickradius; Radius of the joystick moving range
private Vector2 position; The joystick is going to pass the parameters, and he controls the movement.
private int lastfingerid =-1; Finger ID of the last touch
private bool centerballmoving = false; Ball Rocker Mobile Switch
Run method at load time
void Start ()
{
To fit the joystick to a different resolution screen, set the size and coordinates
CenterBall.transform.localScale = new Vector3 (Centerball.pixelinset.size.x/screen.width, centerball.pixelinset.size.y/screen.height,0);
Because the GUI texture anchors are all lower left 0,0, so in order to let the two sides of the rocker symmetrical rocker coordinates to the x direction to move half of the texture width of the screen distance (screen distance through the texture width: screen resolution width obtained)
CenterBall.transform.position = new Vector3 (centerball.transform.position.x-centerball.pixelinset.size.x/2/ Screen.width,
centerball.transform.position.y,0);
The joystick coordinates are assigned to the center of the rocker, which is then used to reset the joystick
Centerpos = centerBall.transform.position;
}
How to run each frame
void Update ()
{
Call the Joystick method
Joystickcontroller ();
}
Joystick method
void Joystickcontroller ()
{
int count = Input.touchcount; Get the number of touch points
for (int i = 0; I count; i++)//analyze touch point actions individually
{
Touch touch = Input.gettouch (i); Gets the currently processed touch point
Converts the current touch coordinates to screen coordinates
Vector2 Currenttouchpos = new Vector2 (Touch.position.x/screen.width-centerball.pixelinset.size.x/2/screen.width, Touch.position.y/screen.height);
Vector2 temp = Currenttouchpos-centerpos; Get Direction vector temp (touch position and joystick's coordinate difference)
if (Centerball.hittest (touch.position))//Determine if the touch point is within the range to be dried
{
if (temp.magnitude Joystickradius)///If the length of the direction vector temp does not exceed the radius of the rocker, Temp.magnitude is the distance between the coordinates difference and the distance between two points
{
Lastfingerid = Touch.fingerid; Record the ID of the touch
Centerballmoving = true; Joystick Move Switch Open
}
}
If the center ball movement switch is turned on, the Joystick Center ball will follow the finger movement. But it needs to be limited, when the finger touches the circular area without exceeding the rocker, the center ball follows the finger touch completely;
When the finger touches out of the circular area, the center ball is in the direction of the touch position and the center point of the joystick and cannot exceed the radius
if (Touch.fingerid = = Lastfingerid centerballmoving)
{
if (temp.magnitude Joystickradius)//If the finger touches a circular area without exceeding the rocker, i.e. the rocker radius, the position of the center ball of the joystick follows the finger
{
CenterBall.transform.position = new Vector3 (currenttouchpos.x,currenttouchpos.y,0); Set the coordinates of the joystick equal to the coordinates of the contact point
}
else//out of radius
{
Vector2 Temp2 = temp; Defining temporary variables Temp2
Temp2. Normalize (); Standardize the Temp2
Set the joystick coordinate position not exceeding the radius
CenterBall.transform.position = new Vector3 ((Joystickradius * temp2 + centerpos). x, (Joystickradius * temp2 + centerpos). Y , 0);
}
if (temp.x = 0)
{
For example code: control the direction of rotation, mainly using Vector2.angle (temp, new Vector2 (0, 5)) to get the angle and take advantage
Initialization_script.current_player_tank_script. Bodyrotation (Vector2.angle (temp, new Vector2 (0, 5)));
}
if (temp.x 0)
{
For example code: control the direction of rotation, mainly using Vector2.angle (temp, new Vector2 (0, 5)) to get the angle and take advantage
Initialization_script.current_player_tank_script. Bodyrotation ( -1 * Vector2.angle (Temp1, New Vector2 (0, 5)));
}
A function that controls movement, or a function that controls fire, assuming that the left joystick controls movement, and the right shakes the rotation
Switch (joysticktype)
{
Case Joysticktype.leftjoystick:
Position = temp*100; Move required coordinates
Break
Case Joysticktype.rightjoystick:
Position = temp*10; Rotation required coordinates
Break
}
Center ball position Reset when Touch is released
if (touch.phase = = Touchphase.ended | | touch.phase = = touchphase.canceled)
{
CenterBall.transform.position = new Vector3 (centerpos.x, centerpos.y, 0); Swing Lever Reset
temp = new Vector2 (0,0); Distance difference is 0
Position = temp; Assign a value of 0 to the required coordinates have stopped the action
Centerballmoving = false; Setting cannot move the lever
Lastfingerid =-1; Clear this time finger Touch ID
}
}
}
}
Get the transitive coordinate method
Public Vector2 getpositions ()
{
return position;
}
}
For more information, please visit the "Dog Planing Learning Network" Unity Ultimate Academy Http://unity.gopedu.com
Statement: This document comes from the "Dog Planing Learning Network" Community-unity Extreme College, is a self-published Unity3d study articles, if anything violates your relevant interests, please communicate with the official, we will deal with the real-time.
Unity3d Dual Joystick C # JoyStick realizes its own double stick