Rotation script issue?

Hey guys, I am trying to make it so when I press A or D I start to rotate until I stop to press W(Forward) again…Not sure what I am doing wrong here…

    if (Input.GetKey(KeyCode.A))
        transform.Rotate(Vector3.up * speed * Time.deltaTime);

    if (Input.GetKey(KeyCode.D))
        transform.Rotate(-Vector3.up * speed * Time.deltaTime);

I have these both in the Update() function…Am I not calling them right? For our game I want it to be a smooth 60 degree turn every-time not sure how to do this or where to find out how…

Thanks!

What do you mean by ‘until I stop to press W again’? This snippet doesn’t include anything that checks the W key, so it’s confusing what you’re asking.

It’s also unclear to me how you want to create a 60 degree turn with this…all you are doing here is checking if the key is down, and applying a rotation every frame. You aren’t checking whether the rotation has gone over 60 degrees, or performing any rotations while the player is not holding down a button.

It sounds like what you want to do is create a coroutine to rotate the player when they press a button; so instead of

if (Input.GetKey(KeyCode.A))
         transform.Rotate(Vector3.up * speed * Time.deltaTime);

you’d do something like

if (Input.GetKeyUp(KeyCode.A))
         StartCoroutine(DoRotation());
...
IEnumerator DoRotation()
{
    float angle = 0;
    while(angle < 60)
    {
        a = rotationSpeed * Time.deltaTime;
        transform.Rotate(a)
        angle += a;
        yield return new WaitForEndOfFrame(); 
    }
}

In the future, you should also use a more descriptive title so other users who want to do this can find your answer. “Perform fixed rotation over time” or something that describes your goal. You should also include a description of the behavior you are currently seeing, compared to what you want so it’s more clear what help you need.

So basically, I’m trying to create a movement script without using a RayCast because this is my first game. I’m trying to make it so when I use A or S I will rotate, but W will move me forward do you know what I mean? I think I meant 60 degrees to keep it a certain way, but trying to get so when I hit D or A I can keep hitting it until I am either forward, backward, left or right, then I can press W to move. Ya know?

Thanks again,

  • KRD