When writing third-person control, the first computer test is to use WASD to control the character.
Later it needs to be posted to the phone, so a joystick is added.
The code for the keyboard control role has been written and the character moves in eight directions.
If according to the traditional popular ideas to control, is not up to the effect I want, the traditional control thinking code is as follows:
1 //When the joystick is moving, the character begins to run2 voidOnjoystickmove (movingjoystick move)3 {4 if(Move.joystickname! ="Easyjoystick")5 {6 return;7 }8 //Get joystick Offset9 floatJoypositionx =move.joystickaxis.x;Ten floatJoypositiony =Move.joystickaxis.y; One if(Joypositiony! =0|| Joypositionx! =0) A { - //sets the orientation of the character (toward the current coordinate + joystick offset) -Transform. LookAt (NewVector3 (transform.position.x + Joypositionx, TRANSFORM.POSITION.Y, Transform.position.z +joypositiony)); the //move the player's position (move by position) -Transform. Translate (Vector3.forward * time.deltatime *7.5F); - //Play run animation -Animation. Crossfade ("Run"); + } -}
If you want to follow the traditional idea of joystick control role, in the re-write control role code is very troublesome, so I calculate the joystick rotation angle to determine the current direction of the stick
OK, now let's start knocking on the code.
First, let's debug the return value of the x-axis and y-axis of the joystick.
1 // move the lever 2 void Onjoystickmove (movingjoystick move) 3 {4 "," + move.joystickaxis.y); 5 }
The debug results are:
Left: x = -1,y = 0; clockwise rotation x becomes larger and y becomes larger
Upper: x = 0,y = 1, clockwise rotation x becomes larger, y becomes smaller
Right: x = 1,y = 0, clockwise rotation x becomes smaller, y becomes smaller
Bottom: x = 0,y =-1; Clockwise rotation x becomes smaller and y becomes larger
We view the rocker Basemap as two semicircular, Upper semicircle and Lower semicircular
So:
When the x-axis moves to the left, x = -1;x axis moves to the right: x = 1;x axis rotates 180 degrees from left to right
When y-axis moves to the left, y = 0;y axis moves to the right: y = 0;y axis rotates 180 degrees from left to right
If you look directly at my debugging results must be a bit dizzy, it is recommended to debug side reference my debugging results, so as to understand
How do we calculate the degree of rotation of the current joystick in the upper left corner?
People who have read a primary school can do it, just see the rocker back-1 and 0 heads are confused, and so do I, long time.
When the joystick moves to the left, it is 0 degrees, 360 degrees (because 360 degrees is a lap, has been back to the point of the distance)
When the joystick moves to the top, it is 90 degrees
When the joystick moves to the right, it is 180 degrees
When the joystick moves to the next, it is 270 degrees
Now that you know how much, it's a lot better.
To derive the formula:
When the x-axis is 1 on the right and the x-axis is 180 degrees, then: 1 * 90 + 90 = 180
The current x-axis rotation angle is: X-Axis return value * 90 degrees + 90 degrees
You think it's over? Too early to use this formula to calculate the rotation angle of the upper half circle
Now we're going to get the lower semicircle rotation angle and then use the upper semicircle rotation angle + The lower half circle rotation angle = Current rotation angle
How do we calculate the rotation angle when the joystick moves to the lower half circle?
We already know that the y-axis is 0 on the left, 0 on the right, and 1 on the bottom, continuing with the formula for the x-axis.
Y Left: 0 * 90 + 90 = 90
Y on: 1 * 90 + 90 = 180
Y under:-1 * 90 + 90 = 0
Y Right: 0 * 90 + 90 = 90
X Left:-1 * 90 + 90 = 0
X: 0 * 90 + 90 = 90
X under: 0 * 90 + 90 = 90
X right: 1 * 90 + 90 = 180
We can draw a conclusion from the calculation results
When the y-axis is less than 90 degrees, the rocker is in the lower semicircle
Lower left when the Y axis is less than 90 degrees and X is less than 90 degrees: 270 degrees + Y axis rotation angle
Lower right when the Y axis is less than 90 degrees and X is greater than 90 degrees: 180 degrees + y axis rotation angle
Train of thought, began to knock code, code is not much, I directly posted up, read the appeal text believe you already know what the code is going on
1 ///Calculating the joystick angle<summary>2 ///Calculating the joystick angle3 /// </summary>4 /// <param name= "_joypositionx" >joystick X-axis</param>5 /// <param name= "_joypositiony" >Joystick Y-axis</param>6 /// <returns>returns how many degrees the current joystick rotates</returns>7 Private floatCalculaangle (float_joypositionx,float_joypositiony)8 {9 floatCurrentanglex = _joypositionx * 90f + 90f;//x-axis Current angleTen floatCurrentangley = _joypositiony * 90f + 90f;//y-Axis current angle One A //Lower semicircle - if(Currentangley <90f) - { the if(Currentanglex <90f) - { - return270f +Currentangley; - } + Else if(Currentanglex >90f) - { + return180f + (90f-Currentangley); A } at } - returnCurrentanglex; -}
OK, now know how many degrees the current joystick rotates, we can easily use the angle to determine the current direction of movement
When using keyboard control:
A = Left
WA = Upper Left
W = Upper
WD = upper Right
D = Right
SD = lower Right
S = Lower
SA = lower Left
When the joystick angle is 0 degrees, go left
When the rocker angle is 90 degrees, go up
When the joystick angle is 180 degrees, go right
Then we must not write this, you can be sure that the player operating the joystick so accurate ah?
Because I control the character here is eight direction, so: 360/8 = 45
Each direction has 45 degrees to trigger, then the following solutions are obtained:
Top: Current angle <= + 45/2 = 112.5 && current angle >= 90-45/2 = 67.5
The following code is obtained:
1 floatCurrentangle =Calculaangle (Joypositionx, joypositiony);2 3 if(Currentangle <=22.5f&& currentangle >= 0f | | Currentangle <= 360f && currentangle >=337.5f)//0; Left4Currentdire ="A";5 Else if(Currentangle <=67.5f&& Currentangle >=22.5f)//45. Top Left6Currentdire ="WA";7 Else if(Currentangle <=112.5f&& Currentangle >=67.5f)//90; up8Currentdire ="W";9 Else if(Currentangle <=157.5f&& Currentangle >=112.5f)//135. Upper RightTenCurrentdire ="WD"; One Else if(Currentangle <=202.5f&& Currentangle >=157.5f)//180; Right ACurrentdire ="D"; - Else if(Currentangle <=247.5f&& Currentangle >=202.5f)//225. Lower right -Currentdire ="SD"; the Else if(Currentangle <=292.5f&& Currentangle >=247.5f)//270; Next -Currentdire ="S"; - Else if(Currentangle <=337.5f&& Currentangle >=292.5f)//315. Lower left -Currentdire ="SA";
Get it done, run it on the phone and try it, that's what I want. Joystick eight-way operation effect
Original link: http://www.cnblogs.com/shenggege/p/4115574.html
Unity3d Easytouch calculates the joystick rotation angle and the joystick eight direction control role