• 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
Question by EmpI · Apr 19, 2016 at 12:38 AM · rotationvelocitydirection

Rotation being forced away from what I want

So I have a script for character movement. I have a script that forces the character to face the direction of movement. Problem is for my character, I need the rotation to be X: 90, Y, 180.

Here is my script. It works fine for what I want to do above, but it forces the X and Y rotation to 0. How do I fix this?

     private GameObject parent;
     private Vector3 dir;
     
     void Start(){
         parent = transform.parent.gameObject; //The object uses its parent's velocity
     }
     
     void Update () {
         Vector2 dir = transform.parent.rigidbody2D.velocity;
         float angle = Mathf.Atan2(-dir.y, -dir.x) * Mathf.Rad2Deg; 
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
     }
 }
 
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Scribe · Apr 19, 2016 at 09:30 AM

Option 1 (easy option): set your object to the child of something else, and then rotate the parent around the z direction.

Option 2: Save your desired rotation in a variable and apply your rotation around z as an additional rotation.

Quaternion initialRot;

 void Start(){
     initialRot = this.transform.rotation; //or possibly Quaternion.Euler(blah blah)
 }
 
 void Update(){
     transform.rotation = initialRot*Quaternion.AngleAxis...;
 }

It is possible that for this, you may need to convert Vector3.forward into local space before rotating around it, see Transform.InverseTransformDirection.

Comment

People who like this

0 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 EmpI · Apr 20, 2016 at 06:29 PM 0
Share

I ended up using both options as it was the only way Unity would allow it to work. I parented it to an object with an X-Y rotation of 90-180 and used initialRot as suggested to get it to work. Still had to monkey around with (-dir.y, -dir.x) part and had to change the Vector3 to Up, but it works now. Thank you!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

49 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

Related Questions

(s)lerp y-rotation to the direction of velocity 0 Answers

Modify rotation based on passing rigidbody's velocity and direction? 1 Answer

Gun not firing in certain direction 1 Answer

Objects Rotation to modify Objects transform velocity/Direction of travel 1 Answer

Teleport and change direction of current velocity. 2 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