Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TWicked · Jul 14, 2017 at 05:29 AM · rotationvelocityanglevector 3

rotate object until rigitbody.velocity is facing the target

Hi, I need a bit of help, i think its a simple problem but i cannot figure it out. Basically what i want to do is rotate object left or right so that the direction of rigidbody.velocity vector is pointing at target object. Or at least how do i find if target object is to the left or right from the direction of my rigidbody.velocity vector. Please note that my transform.forward is not the same as rigitbody.velocity

Any ideas how i can code that?

Thanks in advance

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 carle13 · Jul 14, 2017 at 07:19 AM 0
Share

What do you mean by rigidbody.velocity facing target?

avatar image TWicked carle13 · Jul 14, 2017 at 07:28 AM 0
Share

So that the direction of rigidbody.velocity vector is pointing at target.

avatar image carle13 TWicked · Jul 14, 2017 at 07:37 AM 0
Share

So you want the rigidbody to move towards the target?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by carle13 · Jul 14, 2017 at 06:25 PM

Well, if I understood your question what it seems that you want to do is similar to something I've done for a game. alt text

In my game there are police cars that chase the player, so to make them point to the player's car I check if according to the police's local coordinates the player's car is at x positive which is right or x negative which is left. I do this with two simple lines of code I found in this tutorial https://www.youtube.com/playlist?list=PL67XFC3MYQ6JV0l9485jB1Dpn4atLsFSo The lines are:

 Vector3 steerVector = transform.InverseTransformPoint(nav.steeringTarget);
 steering = maxSteer * (steerVector.x / steerVector.magnitude);

So in the first line it gets the position of the target relative to the police's local system of coordinates. In the second I divide the x component of that vector by it's magnitude, in that way I can know how much of x that vector has. In the image above these are the values I get alt text So as in the code I'm dividing the x component (8.71) by the magnitude (23.57) I get a turn of 0.37 (the division only gives values between 0 and 1). Then I multiply that amount by my maxSteer which is an angle of how tight I want the turns to be.

As another example, if I put the player's car in front of the police car I get the x component (-0.26) and the magnitude (64) which gives me a turn of -0.004 which is almost 0 because it doesn't need to turn as the player is already in front.

And if the local coordinates of your object are different you can just try changing the x component by the y or z component. I may have got wrong the values but I hope you get the idea.


captura.png (52.9 kB)
captura.png (2.2 kB)
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 TWicked · Jul 15, 2017 at 03:20 AM 0
Share

Thanks for your reply. Unfortunately your solution is incorrect for my problem. It would work perfectly if i was trying to have transform.forwards direction of a vector to face target. But i am trying to have direction of rigidbody.velocity vector to face target.

Red arrow is transform.forfwards Vector Black arrow is rigidbody.velocity vector

i need to rotate car till black arrow points at target. alt text

img1.png (47.9 kB)
avatar image carle13 TWicked · Jul 15, 2017 at 03:36 AM 0
Share

So you want to rotate the object to change the direction of it's movement with it, kind of like the object had a propeller on it's back and when you rotate the object the direction that the propeller is pushing the object changes?

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

89 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Velocity to 2D rotation (angle from velocity vector with atan2) 1 Answer

Problems caused by non-unique Euler angle solutions 1 Answer

Restricting rotation to multiples of 15? 1 Answer

Convert Object Rotation to X, Y values 1 Answer

How do I apply rotations to parent child using Rigidbody? 0 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