Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by TRON-dll · Jun 02, 2013 at 10:49 PM · rotation3dcharacterplatformerturning

How can I make an object rotate to a specific euler angle?

Essentially, what I'm trying to accomplish is I want a character to rotate in the direction he will be moving. So if the player holds the move up key, the character will start moving forward as he rotates to that direction.

So the way I've theorized this is that the character will rotate to a set y-axis euler angle. So, in the case of up, 270, right would be 0, and so on. The way I'm doing this right now I feel is a bit inefficient, and doesn't allow for diagonal movement, either:

 else if (Input.GetKey(KeyCode.W))
         {
             if ((transform.eulerAngles.y > 270f && transform.eulerAngles.y <= 360f) || (transform.eulerAngles.y >= -.5f && transform.eulerAngles.y <= 90f))
             transform.Rotate(new Vector3(0, -rotSpeed, 0));
         
             transform.position += (transform.forward * moveSpeed * Time.deltaTime);
         }

If this seems too vague, let me know and I'll attempt to clarify. While my code is in C#, I'm fine with solutions both that and JavaScript. 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fueldown · Jun 03, 2013 at 06:25 PM

 float currentRotation = transform.rotation.eulerAngles.y;
 
 if (Input.GetKey(KeyCode.W))
 {
     transform.Rotate(new Vector3(0f, -rotSpeed * Time.deltaTime, 0f));
     currentRotation = Mathf.Clamp(currentRotation, -90f, 90f);
  
     transform.position += (transform.forward * moveSpeed * Time.deltaTime);
 }

Will this do it for you? I am not sure what type of game you are making, but from description, I am assuming it is a side scroller. I hope this helps.

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 TRON-dll · Jun 03, 2013 at 08:31 PM 0
Share

Thanks. The game I'm making is a 3D platformer. The code you provided didn't work, but perhaps I'm not understanding it correctly.

From what I can see, in this case, when the user is pressing the W key, the game will rotate the object at the rotate speed scaled by deltaTime about the Y axis, and then currentRotation is clamped to the range of -90 to 90 degrees?

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

15 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

Related Questions

How to Make animation Playable? 1 Answer

Changing Animation Rotation offset 0 Answers

Make the character face the direction he is moving? 2 Answers

Making sprites just rotate and not tilt. 3 Answers

Unity Animation Import very slow 4 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