KeyNavas
KeyNavas

Reputation: 172

Character Movement won't work properly because of the rotation

So basically the problem is that when I want to rotate the character and move it the movement wont work properly as expected. if my y coordinates are below zero the character moves backwards instead of forward. So basically in the opposite direction. Also if my coordinates of the joystick are like for example:y=0,23 and x=0,8 my character shows in that direction but moves down right.. PS: The y and x values are from 0 to 1 (unit circle) But If I only move the character without rotating him the character movement behaves as it should.

here is my current code :

 void Update()
 {

     if (myX != leftController.GetTouchPosition.x || myY != leftController.GetTouchPosition.y) { //checks if player changed position.
         myX = leftController.GetTouchPosition.x;
         myY = leftController.GetTouchPosition.y;

         double rad = Mathf.Atan2(leftController.GetTouchPosition.y, leftController.GetTouchPosition.x); // In radians
         double deg = rad * (180 / System.Math.PI); // values from up right to up left : +0 to +180 and from down left to down right: -180 to -0

         double myRotAngle = 0;
         //calculating the specific angle in which the character should move
         if (deg < 0) {
             myRotAngle = 90 + ((deg * -1));
         } else if (deg >= 0 && deg <= 90)
             myRotAngle = 90 - deg;
         else if (deg >= 90 && deg <= 180)
             myRotAngle = 270 + (180 - deg);

         transform.localEulerAngles = new Vector3 (0f,(float)myRotAngle, 0f);//rotate

     }

     _rigidbody.MovePosition(transform.position + (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements) +
         (transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements) );//move
 }

Upvotes: 2

Views: 1431

Answers (1)

Jarrett
Jarrett

Reputation: 196

The problem is that transform.forward is a vector that points in whichever direction your transform (in this case your character) is facing.

Let's say your joystick gives (0,1). Your code will rotate the character to face North and then walk in the direction they are facing. Good.

Let's say your joystick gives (0,-1). Your code will rotate the character to face South then make them walk in the opposite direction they are facing. This will make you go in the same direction as (0,1)!

If what you want is for (0,1) to always move the character North, and (0,-1) to always move the character South, changing transform.forward and transform.right to Vector3.forward and Vector3.right should suffice:

Vector3 forwardMovement = (Vector3.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements);
Vector3 sidewaysMovement = (Vector3.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements);

_rigidbody.MovePosition (transform.position + forwardMovement + sidewaysMovement);

I also split that MovePosition line into multiple steps so it's easier to read.

Another option for your joystick is to have the vertical axis move the character forwards and backwards, and for the horizontal axis to rotate them:

void Update()
{
    float newRotation = transform.localEulerAngles.y + leftController.GetTouchPosition.x;
    transform.localEulerAngles = new Vector3 (0f,newRotation, 0f);

    Vector3 forwardMovement = (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements);
    _rigidbody.MovePosition (transform.position + forwardMovement);
}

Upvotes: 1

Related Questions