- Home /
Question by
veneralan · Mar 08, 2021 at 06:06 PM ·
rotationeulerangleseuler
rotate an object by 180° on click only once
Heyo, so I need to make it so that a 3d-object rotates only once after mouse click. The code I have works, but the object rotates every time I click on it and I need it to do it only once. Code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RotateOnce : MonoBehaviour { public float smooth = 1f; private Vector3 targetAngles;
void Update ()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
targetAngles = transform.eulerAngles + 180f * Vector3.up;
transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, targetAngles, smooth * Time.deltaTime);
}
}
Comment
Answer by pil95 · Mar 08, 2021 at 06:14 PM
You can use a bool here
private bool alreadyRotated = false;
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (alreadyRotated == false)
{
targetAngles = transform.eulerAngles + 180f * Vector3.up;
transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, targetAngles, smooth * Time.deltaTime);
alreadyRotated = true;
}
}
}
doesn't seem to work, but your reasoning is solid. The object with your code keeps rotating every time I click on the screen
Your answer
