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 zloph · Jun 25, 2012 at 08:49 AM · javascriptinputeuleranglesscriptingproblem

Making a character rotate diagonal?

Hi I'm trying to get my character to rotate 45 degrees in the y euler. The character can run in the x and z axis, so its got the right y euler angle for them. What i want is to rotate to 45 degress whenever the character goes both in the x and z axis.

im very new to scripting so i cant really wrapped my head around this.

 function Update () 

{ //rotate in horizontal and vertical planes

 if (Input.GetKeyDown("d") && !(Input.GetKeyDown("w")))
     {
         transform.eulerAngles.y = 90;
     }
 if (Input.GetKeyDown("a"))
     {
         transform.eulerAngles.y = 270;
     } 
 if (Input.GetKeyDown("w") && !(Input.GetKeyDown("d")))
     {
         transform.eulerAngles.y = 0;
     }
 if (Input.GetKeyDown("s"))
     {
         transform.eulerAngles.y = 180;
     }
 //rotate in diagonal planes
 if (Input.GetKeyDown("w") && Input.GetKeyDown("d"))
     {
         transform.eulerAngles.y = 45;
     }
 

}

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
1
Best Answer

Answer by jorjdboss · Jun 25, 2012 at 12:01 PM

You don't have to be fixed on 45 degree increments, its pretty easy.

 function Update () 
 {
     var hor = Input.GetAxis("Horizontal"));
     var ver = Input.GetAxis("Vertical"));
 
     // Depends on the default facing direction, which is usually the character facing forward z axis
     if(hor < 0)
        transform.eulerAngles.y = Vector2.Angle(Vector2(0,1), Vector2(hor, ver));
     else if(hor > 0)
        transform.eulerAngles.y = -Vector2.Angle(Vector2(0,1), Vector2(hor, ver));
     // ignore when hor = 0
 }

You'll get what I did if you've done some basic Vector algebra. If you didn't, I'll give a brief explanation:


Imagine there's a 2d plane(X and Y axes) on the floor and character is standing on it. The characters faces this imaginary Y axis and his right hand faces the X axis.


The angle between a reference point (1,0) and the user inputs on the horizontal and vertical axes is the angle your character should be facing. I'm assigning the negative angle when its obtuse( > 180) because Vector2.Angle() always returns the smallest angle which is always < 180

Hope you find this useful

Comment
Add comment · Show 2 · 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 zloph · Jun 25, 2012 at 12:40 PM 0
Share

Thank you for that detailed answer, very useful indeed. With a few bits of tweaking it works better than what I wanted. Never thought about using input getaxis and vector2 that way. :)

avatar image jorjdboss · Jun 26, 2012 at 05:42 AM 0
Share

Glad to know you found my explanation useful. Unity has more such tricks people aren't aware about or forget. I'd say get a good grip on the documentation and you're golden! This is my way of doing it, there might be other ways of doing it without using vector2, go figure :) It'd be nice if you gave a vote up(my first vote!)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I invert the Y axis in Penelope tutorial? 3 Answers

Joystick ellipse movement (2d mobile asset modification) 1 Answer


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