The camera and EasyTouch can control the moving direction of the character at any time
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
//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 z-axis of the camera changes
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 ");
Bug