Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by goldeniotagaming · Aug 09, 2016 at 05:35 PM · c#unity 5rotation

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image goldeniotagaming · Aug 10, 2016 at 04:28 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

236 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges