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 geroppo · Dec 28, 2012 at 09:31 PM · rotationrotateaxisy

Rotation in 8 way direction

Hello there, and first of all i know that there are ALOT of questions about rotation already answered, but i think that none of them seems to answer my problem ( i tried alot of things that came out of those ).

The thing is quite simple, using the arrows or WASD keys i want an object to rotate in the Y axis towards the same direction while having a static camera ( so i dont need a rotation based on the camera ). this is in a 3D way like the old games , for example : i press "up" and the object looks at 0º, i press "right" and the object looks at 90º, i press "up" and "right" and the object looks at 45º ( here is my problem ).

So far i have this code working but it seems pretty poor because it only works when i press "up " and "right" at the EXACT SAME time, otherwise if i press "up" and THEN press "right", it will still be looking at 0º.

 if (Input.GetButtonDown("Up"))
         {
      transform.rotation=Quaternion.Euler(0,0,0);
     }
 if (Input.GetButtonDown("Right"))
     {
      transform.rotation=Quaternion.Euler(0,90,0);
     }
 
 // and here is what i can't figure out how to do it correctly
 // tho i know that all this could be made in a different way
 
 if (Input.GetButtonDown("Up") && Input.GetButtonDown("Right))
         {
         transform.rotation=Quaternion.Euler(0,45,0);
         }

Thanks for any kind answer and sorry if someone asked something similar ( thought i looked around 20 other questions before doing this one to make sure no one asked this already)

PS: as you can see, i just want the object to change abrutly its rotation as the old good games, so there is no need to "smooth" the thingy. And i have already configured the input keys.

Comment
Add comment · Show 2
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 Golan2781 · Dec 28, 2012 at 09:40 PM 0
Share

Is there a reason why you are not using the Input$$anonymous$$anager axis functionality? You could simply extract an orientation if you were using two axes of Input using left/right and up/down; we're using it for a similar use-case. Something like
transform.rotation=Quaternion.Euler(0,arctan(Input.GetAxis ("LookHorizontal")/Input.GetAxis ("LookVertical")),0);

That would cover all cases with a single function.

avatar image geroppo · Dec 28, 2012 at 10:25 PM 0
Share

i used this for nothing in particular, i thought it would be easier this way ( dividing the axis functionality in 4 ), it was just the first thing that came to my $$anonymous$$d. Btw can you explain a little more the functionality of

arctan(Input.GetAxis ("LookHorizontal")/Input.GetAxis ("LookVertical")) ? thanks for your reply

2 Replies

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

Answer by save · Dec 28, 2012 at 10:28 PM

You could assign a new forward direction based on the horizontal and vertical axes.

 function Update () {
     if (Input.GetButton("Horizontal")||Input.GetButton("Vertical")) {
         var newForward : Vector3 = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
         if (newForward!=Vector3.zero) transform.forward = newForward;
         transform.Translate(Vector3.forward*Time.deltaTime);
     }
 }
Comment
Add comment · Show 4 · 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 geroppo · Dec 28, 2012 at 10:39 PM 0
Share

wow it worked pretty good thanks! i didn't thought about using forward direction

avatar image geroppo · Dec 28, 2012 at 10:41 PM 0
Share

sorry to ask one more thing, i noticed that when you go into the opposite direction, the console says "Look rotation viewing vector is zero", its not a problem but i wonder why

avatar image save · Dec 28, 2012 at 10:46 PM 0
Share

Ah yes sorry, that happens when trying to set the forward direction to zero. Use the updated code sample to fix that issue.

avatar image geroppo · Dec 28, 2012 at 10:51 PM 0
Share

thank you very much! works like a charm

avatar image
0

Answer by Golan2781 · Dec 28, 2012 at 10:53 PM

What you want to do could be easier done using Input axes. Go to Edit -> Project Settings -> Input and define two axes here, one for left/right and one for up/down. Once you got that, the two axes can be seen as a direction vector. The construct

 Vector2((Input.GetAxis ("Horizontal"),(Input.GetAxis ("Vertical"))


would for example point up with you'd press up or point to the lower right if you'd press down and right.

Seeing how you're only interested in the angle of the direction, you can calculate that directly using trigonometric functions. Arctan is the correct one in your situation; unity calculates the arctan in radians, so you also need to convert it to degrees.

 Mathf.Atan((Input.GetAxis ("Horizontal")/Input.GetAxis ("Vertical"))*Mathf.Rad2Deg
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 geroppo · Dec 28, 2012 at 11:09 PM 0
Share

thank you for your explanation, i did have those inputs but i was very confused about how to use them , so i divided them in 4 :P

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

9 People are following this question.

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

Related Questions

how to know if an axis has been full rotated? 1 Answer

How can i rotate the aircraft elevator around the yellow axis up and down ?? as shown in the pic 0 Answers

Lock the Camera's Y Rotation Axis? 0 Answers

Rotating an object on Z axis while moving 1 Answer

Moving an object based on the position of another object 2 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