The camera and EasyTouch can control the moving direction of the character at any time

The EasyTouch plug-in is a good touch screen control plug-in. The joystick of many Unity games is made with EasyTouch.

Reminder: This was written when I learned Unity by myself when I was in school. Now EasyTouch has an internal interface to take coordinates, angles, etc.
This is a 2017 article, so don't ask me again.

Camera z-axis unchanged

In some Camera pointing is fixed (relative to the world coordinate system) In the game of (such as King's Glory, etc.), you can normally control the character to move up, down, left and right using the common method of EasyTouch. The code is similar to the following:

 //Get the rocker coordinates of EasyTouch Vector2 joystickPosition = joystick.JoystickAxis; //Character turn this.transform.LookAt(transform.postion + new Vector3(joystickPosition.x,0, joystickPosition.y); //The character moves to transform. forward (that is, the direction vector of the character's orientation in the world coordinate system) //Transform. The second parameter of Translate defaults to its own coordinate system, so you can directly use Vector3.forward. this.transform.Translate(Vector3.forward * Time.deltaTime);

The principle of this method is as follows:

The z-axis of the camera changes

If the camera is used to follow the third person person and Camera direction is synchronized with person direction Some problems may occur with the above movement. For example, my Camera z-axis always follows character z-axis , dragging the rocker will produce Unexpected character movement When the character faces the opposite direction of the world coordinate axis z axis (the camera z direction is synchronized with the character z direction), push the rocker upward, and the character will instantly turn to the positive direction of the world coordinate axis (that is, the opposite direction the character faced before) and start moving. This is certainly not the effect we expected.

Normally, The desired effect of the user is that when the rocker is pushed upward, the character moves forward, that is, it moves in the positive direction of the z-axis of the local coordinate system , as shown in the figure:

Think about the broken 3rd that you played everyday, and think about logic.

The problem is that when the character turns to this step, our The steering standard is relative to the world coordinate system (see Figure 1 for details) Therefore, when we pull up the rocker, we will instantly look At to the positive direction of the z axis of the world coordinate system, which is not in line with the user's operating habits. To solve this problem, We need to rotate the character's rotation angle with reference to the local coordinate axis The key point is Get the rotation angle of the character corresponding to the rocker The schematic diagram is as follows:

As long as the rotation angle in the figure is obtained, let the character rotate according to its own y-axis, so The z-axis of the character will point in the direction we expect The following is my method for calculating the rotation angle.

 float joystickX = joystick.JoystickAxis.x; float joystickY = joystick.JoystickAxis.y; float rotateY = 0f; if(joystickY > 0) { //The first and second quadrants //The positive, negative and size of the angle values of the first and second quadrants are consistent with our needs, which can be solved normally //Mathf. Atan calculates and returns the arctangent of the number specified in parameter f. The return value is between negative half pi and positive half pi. rotateY = Mathf.Atan(joystickX/joystickY) * 180/Mathf. PI; }else if(joystickY < 0 && joystickX < 0) { //Third quadrant //Add the included angle between the rocker axis and the negative direction of y-axis with - 180 ° rotateY = -180 + Mathf.Atan(joystickX/joystickY) * 180/Mathf. PI; }else if(joystickY < 0 && joystickX > 0) { //Quadrant 4 //180 ° plus the included angle between rocker axis and y-axis negative direction rotateY = 180 + Mathf.Atan(joystickX/joystickY) * 180/Mathf. PI; } this.transform.Rotate(0, rotateY  * turnSpeed /180,0); cc.SimpleMove(this.transform.forward * 5); ani.CrossFade(" Running@loop ");

effect:

Bug

When the rocker is in the third quadrant, if the rocker is moved to the fourth quadrant, it will rotate clockwise (the negative angle will return to 0 and continue to increase to the positive angle), but the result of rotation is correct, harmless, and I am too lazy to change it.

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/2570.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. I think I want to ask some questions from QQ, the big guy

    • hare 10-27 20:17 reply

      Click the QQ icon in the lower right corner of PC browser to add me as a friend!

  2. xema 10-27 20:22 reply

    What kind of game is Brother Rabbit going to do

  3. Time House 10-29 16:13 reply

    My legs are so short, I almost laughed

  4. Hahaha Daily walk

  5. Wenxi 10-31 21:11 reply

    Happy Halloween

  6. c0smxsec 11-05 00:10 reply

    walk about

  7. xema 11-05 19:44 reply

    The short legs are so cute