- Home /
Question by
savanluffy · Sep 08, 2017 at 11:59 PM ·
anglerotate objectrotation axisrotatingangleaxis
Rotate a gameobject by a specific angle and stop when character doesnt move anymore
So basically what I am trying to do is when I move my character via joystick I want to calculate the current angle the character is rotating and the new angle in which he should rotate. for example: i am currently moving at 45 degrees. now I move the joystick to 90 degrees. So basically I want to calculate the difference between the current and new angle and than rotate at that specific angle. In this case it would be 45 to left.
Here is where I calculate the new degree:
void Update()
{
// move
_rigidbody.MovePosition(transform.position + (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements) +
(transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements) );
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 difference =....;
// transform.Rotate(Vector3.up,(float)difference * Time.deltaTime);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612134447im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Strange rotation pattern. 0 Answers
How can I rotate an object without moving it up or down? 0 Answers
Shooting Direction with UI joystick 1 Answer
Why is this rotation not performed as expected? 1 Answer
Weird Output from AngleAxis 0 Answers