In my project I have a script that moves my player forward as I hold down the mouse button and another part that rotates them 180 degrees upon press. My problem is that when my character moves while rotating, the movement will “snake”. What I want to do is while moving forward you can right click to turn around and move along the same “line” in the opposite direction. Killer7 is a game that does this for example.
Here is my code:
public float moveSpeed = .25f;
public float smooth = 1f;
public float turnRate = 0.5f;
private float spinDelay;
private Quaternion targetRotation;
void Start ()
{
targetRotation = transform.rotation;
}
void Update ()
{
if(Input.GetMouseButton(0))
{
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
}
if (Input.GetMouseButtonDown (1) && Time.time > spinDelay)
{
spinDelay = Time.time + turnRate;
targetRotation *= Quaternion.AngleAxis (180, Vector3.up);
}
transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation , 10 * smooth * Time.deltaTime);
}