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
2
Question by EmpI · Nov 23, 2014 at 07:39 PM · rotationvelocity

Rotate Object to face Velocity (2D)

So I have a script that I want to force the object to look in the direction its velocity is taking it. (or possibly 180 degrees of it, depending on the orientation of the 2D collider it's using.)

However, it seems at random, the object snaps to 90 degrees for no apparent reason, and the collider points in an odd direction.

So i'm at my wit's end here, anyone have any idea what to do?

Here's the code that forces rotation in velocity (Note that the object will bounce around, so direction changes will occur)

     private Vector3 dist = new Vector3();
     private Vector3 distprevframe = new Vector3();
     private Vector3 dir = new Vector3();
 
     void Update () {
         dist = transform.position;
         dir = dist - distprevframe;
         dir = dir * 90;
         distprevframe = transform.position;
 
         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis(angle + 90, Vector3.forward);


Also, since I think it's relevant, here is the code for the object being spawned

                 Rigidbody2D Shot;
                 Shot = Instantiate (ProjectileTest, target.position, Quaternion.Euler (Vector3.forward)) as Rigidbody2D; //target is where the object is spawned.
                 Shot.velocity = transform.TransformDirection (Vector3.forward * bulletspeed);
                 Shot.transform.Rotate (Vector3.forward);
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
4
Best Answer

Answer by robertbu · Nov 23, 2014 at 07:59 PM

For 3D object (even in 2D space), @JeffKesselman's answer is in the right direction. But if your object is a sprite or a quad, then construct your sprite so that the front of the object faces right when the rotation is (0,0,0), then you can do:

  Vector2 dir = transform.rigidbody2D.velocity;
  float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
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 EmpI · Nov 23, 2014 at 09:56 PM 0
Share

This method worked the best. Thank you for your help.

avatar image Talhahsarkar · Aug 31, 2018 at 07:41 AM 0
Share

You are the best . . my logic was same as you the only problem which i never thought of was the default sprite orientation . . and when i constructed my sprite to face right it worked!! Thank you so much

avatar image
0

Answer by Jeff-Kesselman · Nov 23, 2014 at 07:45 PM

This is all much too pointlessly complicated.

The vector of your motion is your velocity normalized.

So try something like this:

 Vector2 motionDirection = transform.rigidbody2D.velocity.normalized();
 transform.rotation = Quaternion.LookRotation(motionDirection, new Vector3(0,0,1));

or you can find the point at the end of the vector and use LookAt

  Vector2 lookAtPoint = transform.rigidbody2D.velocity.normalized();
 lookAtPoint += transform.position;
  transform.LookAt(lookAtPoint,new Vector3(0,0,1);

The moral of this story is that vector algebra is a very good thing to know if you are working with a matrix/vector based system.

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 robertbu · Nov 23, 2014 at 08:01 PM 0
Share

@Jeff$$anonymous$$esselman - 'normalized' is not a function, and for a LookRotation() you don't need to normalize the vector.

avatar image Talhahsarkar robertbu · Aug 31, 2018 at 07:42 AM 0
Share

yeah its Normalize()

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

rotate object until rigitbody.velocity is facing the target 1 Answer

Some lines of codes are ignored once certain "if" statement(s) initiated. 3 Answers

[HELP] Rotate Bullet Script 0 Answers

Rigidbody slowing right down when colliding only when I'm rotating the velocity to always be going forward 1 Answer

Adjusting Roll and Pitch with Velocity 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