Not able to get ball to slerp rotate 180 degrees one way on input
Ive been trying to get this to work correctly for quite some time now and have not been able to find anything to help. What Im wanting is the sphere to rotate 180 degrees with a set speed every time the s key is pushed. Since its the same key, the sphere will have to rotate left to right each time s is pushed. I will eventually add in it rotating the other way but want to get this worked out first. Currently Im having two issues with my script. First issue is it works for the first 180 degree rotation but then does not rotate anymore. Now i know this is due to the Slerp never actually reaching 180, so how would i get it so that once it is within a few degrees of 180 it snaps to 180 perfectly? Secondly when i do set the rotation to 180 and press s the ball rotates right to left. What can i do to prevent this and get the same rotation each time. Any help would be much appreciated as i have been working/looking on this for some time now.
public class RotateBall : MonoBehaviour
{
public float smooth = 1f;
private Quaternion targetRotation;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.S) && transform.rotation.eulerAngles.y == 0)
targetRotation = Quaternion.Euler(0, 180, 0);
if (Input.GetKeyDown(KeyCode.S) && transform.rotation.eulerAngles.y == 180)
targetRotation = Quaternion.Euler(0, 0, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, smooth * Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
Smooth rotation on Y axis to target pose 0 Answers
Delayed LookAt, but only on one axis 1 Answer
Slerp rotation issue when calling a lot 0 Answers
Object rotation naturally change 0 Answers
How to control rocket pathing with Quaternion.Slerp 1 Answer