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 charglerode · Aug 02, 2014 at 01:39 AM · physicsrigidbodyvector3joystickvector2

Joystick to turn object

I have an onscreen joystick that spits out a Vector2 for horizontal and vertical movement. The range is -1.0 to 1.0 What I need to do is based on that vector, calculate the rotation of an object to face the direction of the joystick and also to move at a rate based on how far the joystick is moved in that direction. An example would be if the joystick is half the way down and half way to the right, the horizontal value would be 0.5 and vertical would be -0.5. I need to translate that so the object would rotate 135 degrees to face down/right and move in that direction at half speed. The object is a rigidbody so ideally I'd like to use force to rotate and move since this is how it moves already.

Any help would be appreciated.

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

Answer by robertbu · Aug 02, 2014 at 01:47 AM

To calculate the angle, you an use Mathf.Atan2(). I believe the onscreen Joystick gives you back a Vector2. So:

 var angle = Mathf.Atan2(v2.y, v2.x) * Mathf.Rad2D3g;

Note that the parameters are y,) not x,y. Also note that the angle starts at 0.0 on the right and goes counterclockwise, so your values of -.5 and. 5 would generate a rotation of -45 which is 315 degrees. Note if you are setting the direction of a Sprite or a Quad for a 2D game, and the front of your character is to the right, you can set the rotation like this:

 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

...and the angle will match the joystick position.

You can use the magnitude of the Vector2 to scale the movement. There is a slight problem with solution. If the joystick is at an angle, the max value will be sqrt(2), where a horizontal or vertical position will produce a max value of 1.0. So you may want to cap the value at 1.0 or do some other geometry so that a diagonal movement is not faster than horizontal or vertical movement.

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 charglerode · Aug 02, 2014 at 03:27 AM 0
Share

I had to offset the output by 90 degrees because my object is treating up as 0 degrees, but otherwise this works perfectly.

Another question. Ins$$anonymous$$d of just setting the rotation, what is the proper way to add force rotation to the rigidbody until it reaches the destination angle?

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

2 People are following this question.

avatar image avatar image

Related Questions

Apply rigid body force in opposite direction of collider 1 Answer

Constrain rigidbody movement to spline at high speeds / sharp curves 3 Answers

Changing 1 parameter of rigidbody.velocity (Vector3) 2 Answers

Rigidbody object intersection 0 Answers

Rigidody.velocity = Direction * speed; How to get direction? 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