Question by
davidjhensel · Apr 24, 2019 at 07:40 AM ·
c#rotationsnapping
Smooth snapping rotation
Hey, total noob here. I have a series of game objects that need to rotate 90 degrees when input button is pressed. Currently I have the following:
public float smooth = 1f;
private Quaternion targetRotation;
void Start()
{
targetRotation = transform.rotation;
}
void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.DownArrow))
{
transform.rotation *= Quaternion.Euler(0, 90, 0);
}
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, 10 * smooth * Time.deltaTime);
}
This kinda works but its snapping back to the original rotation (0,0,0), where I need it to stop at 90 so that it can continue to rotate. to 180, etc... Cheers
Comment
Best Answer
Answer by xxmariofer · Apr 24, 2019 at 08:27 AM
change thetarget rotation and the fixedupdate code to this
Vector3 targetRotation;
private void Update()
{
if (Input.GetKeyDown(KeyCode.DownArrow))
{
targetRotation += new Vector3(0, 90, 0);
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(targetRotation), 10 * smooth * Time.deltaTime);
}