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 The-BOOM · Sep 11, 2011 at 01:42 AM · physicsrotatetorque

Rotate to look at with physics

I want to be able to have something to rotate to look at something, but I don't know how to make it accurate. I only want it to rotate on the Z axis. Any good way I could do this?

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

Answer by aldonaletto · Sep 11, 2011 at 02:06 AM

You can calculate the direction of the target and zero the z coordinate - this restricts the direction to the XY plane. Then you can calculate a Quaternion.FromToRotation from what you consider your forward direction to the target - if the forward direction of your object is the X axis, for instance, you should use Vector3.right as from direction:

var target: Transform;
...
    var atDir = target.position-transform.position;
    atDir.z = 0; // restricts direction to the XY plane
    transform.rotation = Quaternion.FromToRotation(Vector3.right, atDir);
...
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 The-BOOM · Sep 11, 2011 at 03:08 AM 0
Share

I want to rotate with physics, not with transform.

avatar image PrimeDerektive · Sep 11, 2011 at 03:44 PM 1
Share

You have no reason to rotate with physics if you know the end result you're looking for. Physics should be used for unpredictable simulations. The results will be the same, you'd just be wasting CPU cycles using the physics engine.

avatar image peaj_metric PrimeDerektive · Mar 10, 2016 at 11:12 AM 1
Share

@PrimeDerektive: Only because you can't think of a reason that does not mean there is none. Even if you know the end result sometimes the route is the goal. For exampe if the object you want to turn can collide with other objects. $$anonymous$$g. a spaceship trying to face a certain direction while being hit be asteroids. Thats a pretty tough problem btw.

avatar image peaj_metric · Mar 10, 2016 at 05:29 PM 0
Share

@PrimeDerektive: Only because you can't think of a reason that does not mean there is none. Even if you know the end result sometimes the route is the goal. For exampe if the object you want to turn can collide with other objects. $$anonymous$$g. a spaceship trying to face a certain direction while being hit be asteroids. Thats a pretty tough problem btw.

avatar image
0

Answer by Crooked97 · Jan 23, 2017 at 02:34 PM

This works prefectly fine, no glitches so far.

 void AimAtTarget(Vector2 target, float aimSpeed) {
 
         //getting the angle of the this -> target vector
         float targetAngle = Mathf.Atan2(target.y - transform.position.y, target.x - transform.position.x) * Mathf.Rad2Deg;
 
         if (rb.rotation + 90 >= 180) {
             rb.rotation = -270;
         } else if (rb.rotation + 90 <= -180) {
             rb.rotation = 90;
         }
 
         //getting the angle between the this -> target and the rigidbody.rotation vector
         float diffAngle = targetAngle - (rb.rotation - 90);
 
         if (diffAngle > 180) {
             diffAngle -= 360;
         } else if (diffAngle < -180) {
             diffAngle += 360;
         }
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 Vultus · Nov 26, 2021 at 08:42 AM 0
Share

You can't use summation operations between quaternion and int. How this work perfectly fine ?

avatar image Bunny83 Vultus · Nov 26, 2021 at 12:21 PM 0
Share

Even though it's not obvious from the answer, this answer is about 2d physics. Rigidbody2D does have a single float value as rotation since it's in 2d space where you can only rotate in the x-y plane.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

add force to rotation 1 Answer

AddTorque in 2D doesn't work? 0 Answers

Adding torque in FixedUpdate 0 Answers

simultaneously apply torque around 2 different axes 1 Answer

Without affecting Velocity, what is the best method to apply direction to an object moving with forces? 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