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 siddharth3322 · Nov 28, 2014 at 05:36 AM · physicsvelocitydirection

Find angle from velocity

I want to set angle based on gameobject velocity for 2d game. At present in game, object moves in correct direction but with wrong angle. If I can able to set angle from its velocity then my problem is solved but I can't able to find solution for that.

I already used google for this purpose but didn't find any useful content. Please give me some suggestion in this.

Comment
Add comment · Show 4
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 HarshadK · Nov 28, 2014 at 05:47 AM 0
Share

What is the relation between the velocity and the angle? How do they affect each other?

avatar image siddharth3322 · Nov 28, 2014 at 06:22 AM 0
Share

I just want to send angle angle of my copter in direction it is going. That was my motto behind this question.

avatar image Yalfbal · Nov 28, 2014 at 09:21 AM 0
Share

What are you using to move the copter?

avatar image siddharth3322 · Nov 28, 2014 at 09:23 AM 0
Share

Details related to moving, you can find in following question Copter movement. At present I have correct velocity value in which direction player is moving.

2 Replies

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

Answer by Yalfbal · Nov 28, 2014 at 09:32 AM

Maybe you can use something like:

  Vector2 dir = rigidbody.velocity;
  float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

This works like a transform.LookAt() for 2D.

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 siddharth3322 · Nov 28, 2014 at 10:34 AM 0
Share

Awesome man, you have done your work. Thanks for your help.

avatar image
0

Answer by taxvi · Nov 28, 2014 at 06:44 AM

let's say the if velocity = 0 than angle = 0 else if velocity = maxSpeed than angle = maxAngle:

 transform.eulerAngles.z = (rigidbody.velocity.magnitude / maxSpeed) * maxAngle;
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 taxvi · Nov 28, 2014 at 06:47 AM 0
Share

but thing is, you can not modify separate values of eulerAngles, you have to 1. copy xyz values in a temporary Vector3, 2. modify Vector3 3. assign the modified value back to eulerAngles:

 Vector3 temp = transform.eulerAngles;
 temp = (rigidbody.velocity.magnitude / maxSpeed) * maxAngle;
 transform.eulerAngles = temp;
avatar image siddharth3322 · Nov 28, 2014 at 08:07 AM 0
Share

@taxvi, Thanks for your help but this solution not working for me because my copter object moving in all direction means it can able to take round also.

One more thing, Copter move in whatever direction it always moving with same magnitude so we can't use magnitude for angle calculation.

avatar image taxvi · Nov 28, 2014 at 08:25 AM 0
Share

ok, my bad, use transform.localRotation.eulerAngles.z ins$$anonymous$$d, this will always rotate forward/backward does not matter which way the copter is facing

to smoothly rotate the copter

 currentAngle = $$anonymous$$athf.Lerp(currentAngle, desiredAngle, speedOfRotation);

avatar image siddharth3322 · Nov 28, 2014 at 08:57 AM 0
Share

Yes you are right in this but at present I don't have correct desiredAngle. I can't able to calculate it because of what for that you have to read my previous comment.

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

28 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

Related Questions

How can I convert velocity/direction to Force? 3 Answers

How to calculate the speed and direction to intercept with a target 1 Answer

Finding Velocity at point but locally.... 1 Answer

is there a way to check the path of a RigidBody.velocity? 1 Answer

Add velocity relative to ground? 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