- Home /
Character Movement won't work properly because of the rotation
So 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
}
So basically the problem is that when I want to rotate the character and move it the movement wont work properly as expected. for example if I move my joystick down left my character moves up middle.
But If I only move the character without rotating him the character movement behaves as it should.
Answer by Vicarian · Sep 12, 2017 at 08:53 PM
You're applying the rotation to the character, but when you translate, you're not respecting that rotation - instead going with the raw values off the controller. This makes transform.forward and transform.right constantly move along 0, 0, 1 and 1, 0, 0 respectively. In other words, your forward and right vectors never update their orientation with respect to your rotation. To fix this, use the rotation of the transform instead of your raw controller values in your translation statement, and you should be good to go. Keeping in mind you're working with Vectors, you may simplify the statement:
// transform.right + transform.forward simplifies to (1, 0, 1)
// This is the axis needed by the Quaternion driving the rotation, to test visually:
// Create a cube in Unity
// Set its position to 1, 0, 1
// Rotate some degrees in Y
// Drag the blue translation gizmo (the forward axis in Unity)
// You will see that only the X and Z positions change,
// and it moves strictly along a ray pointed to by the blue arrow,
// which I think is the intended behaviour.
// If you need to add controller input to control both axes,
// simply scale by the controller value:
// (transform.forward * RawYAxisValue + transform.right * RawXAxisValue)
_rigidbody.MovePosition(transform.localRotation * transform.localPosition + (transform.right + transform.forward) * Time.deltaTime * speedMovements);
Vicarian I get the error that I can't use '*' when dealing with vector3 or quaternion
The order matters, I got it backwards. It's:
_rigidbody.$$anonymous$$ovePosition(transform.localRotation * transform.localPosition + (transform.right + transform.forward) * Time.deltaTime * speed$$anonymous$$ovements);
Apologies, I can never remember the order unless I get that error.
Vicarian I don't get the error anymore but it still wont work. When I try to move everything around the character rotates 360° all the time..... When I try to go for example to the right side the character moves a little and than stops and when it stops everything starts rotating
Your answer
Follow this Question
Related Questions
gameobject stops moving correctly when rotating 1 Answer
Determining the rotation of an object based on its upcoming position? 1 Answer
How can you make a 2D object move based on rotation, and rotate while moving? 1 Answer
how to rotate a gameobject in a given direction and stop when it's done 1 Answer
Rotacionar objeto 0 Answers