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!
Answer by Dinosaurs · Aug 09, 2016 at 06:09 PM
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(); 
     }
 }
https://docs.unity3d.com/Manual/Coroutines.html
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.
It still didn't work, would I call the IEnumerator in the Awake() function? Then call the actually coroutine in the FixedUpdate()? I am trying to get it so when I press A or D it will keep rotating 60 degrees every frame until I stop pressing it ya know? So that way I only use W to move forward, I have my actual movement set up like this.
     if ($$anonymous$$athf.Abs(forwardInput) > inputDel)
     {
         //move 
         rBody.velocity = transform.forward * forwardInput * ForwVel;
     }
     else
         rBody.velocity = Vector3.zero; 
 }
P.S Sorry about the title / descriptor I am new to unity forums, thanks again.
Answer by goldeniotagaming · Aug 10, 2016 at 03:49 PM
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 
Your answer
 
 
             Follow this Question
Related Questions
Why isn't my object lerping ? C# 2 Answers
How to reduce the speed of rotation? 2 Answers
Flip 3D Character Rotation 180 on Y axis 1 Answer
Problems With .rotate behavior 1 Answer
Rotation Changing Z-Coordinate Value 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                