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 ac21217 · Dec 23, 2013 at 10:22 PM · rigidbodyvector3lookat

How do I get one object to point toward another using rigidbody physics?

I did some searching and only found answers that involved transform.rotate and such. I'm looking to do the same thing but while maintaining the physics system. I imagine this will involve using AddRelativeTorque.

The transformation takes place in three dimensions and needs to take place over time. I've tried a bunch of different angles on this one, any help is much appreciated.


Found this:

http://answers.unity3d.com/questions/417920/how-to-make-a-rigidbody-rotate-towards-an-object-u.html

But it only works with 1 dimension. Need help implementing for 3 dimensions

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

2 Replies

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

Answer by sparkzbarca · Dec 24, 2013 at 12:02 AM

so are you doign this as 1 rotation or 3? (a realistic turret for example would use 3 seperate axes to move to it, a spaceship with RCS thrusters would find the axis of rotation and apply a single rotation along that axis)

Spaceship

 StartVector = AIM_AT.transform.forward;
 
 EndVector = (AIM_AT.transform.position - POINTING_OBJECT.transform.position).normalized;
 
 AXIS_TO_ROTATE_ON = vector3.cross(EndVector,StartVector);

 //angle is FYI you may not actually require this bit of information 
 ANGLE = Mathf.Atan2(Vector3.Magnitude(AXIS), Vector3.Dot(Startvector, EndVector));

so now your just going to rotate along

I assume you know how to do the calcuations for 3 axis (just ignore an axis each time and find each 3)

If your issue is how do i start and stop the rotation.

well you simply apply torque along the axis which is what we computed above so

.addrelativetorque(AXIS_TO_ROTATE_ON * speed)

so now we have it rotating properly on that axis

now comes another decision

do you want a constant turn rate with a sudden stop or a kind of curve where you approach it and gradually settle on your target.

if you want a sudden stop just do a

//bear in mind doing vector3 == vector3 isn't actually dead accurate, answers taht are close but not exact work, FOR FLOATING POINT NUBMERS and vector3 is 3 floating point numbers floatinga == floatingb acutally means is floatinga APPROXIMATELY floatingb this actually is ok though, you cant hit it aiming perfectly in a frame nessacarily but if your really close to aiming at it it'll trigger the stop.

if(transform.forward == EndVector) { //apply a counter torque so get the current torque and apply that exact same amount of force on the -axis }

if you want to gradually approach you can use a lerp/slerp

you can also use the angle caclatuion above or vector3.angle (might not work as well) to basically take the angle and be like as the angle approaches 0 speed used in addrelativetorque appraoches 0.

anyways there are some ideas.

Comment
Add comment · 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
4

Answer by robertbu · Dec 23, 2013 at 11:50 PM

@Seth's answer uses Rigidbody.MoveRotation(). If MoveRotation() is good enough for what your are trying to achieve, then you can just do:

 #pragma strict
 
 var target : Transform;
 var speed = 5.0;
 
 function FixedUpdate() {
     var qTo = Quaternion.LookRotation(target.position - transform.position);
     qTo = Quaternion.Slerp(transform.rotation, qTo, speed * Time.deltaTime);
     rigidbody.MoveRotation(qTo);
 }

Note it would be much more complicated to use "real" physics and adding torque to solve this problem. What needs to be done will depend on what you are trying to achieve here.

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 iansnyder · Feb 23, 2017 at 07:01 AM 0
Share

This is exactly what I was looking for, thanks!

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

20 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

Related Questions

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

2d game - looking toward velocity for rigidbody player 1 Answer

Make an FPS gun point at the mouse position. 2 Answers

Rigidbody to follow player around 1 Answer

Find the Vector to divert from colliding? 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