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 p_unity · Feb 26, 2015 at 11:38 AM · rotationlookatlookrotationlocalrotationmatching

Match rotation of gameobejcts in one (Y) axis

Hey guys. Might seem a simple one but i do sometimes have trouble with these kind of rotation problems.

My end goal is to make sure all my NPCs are facing the right way when they move around the scene by always facing there current waypoint in a pathfinding script. As before they were gliding around with no relative rotation.

Ive tried ta few approaches none of which work how i need. Ill share them all to hopefully get some guidance on which one (if any) is the closest the actual approach i need.

Firstly i have a seperate child object which just contantly looks at the looks at current waypoint, and then i want to set my actual rotaion on the Y axis to match that rotation. However it seems i cant do this.

 compass.LookAt(path.vectorPath[currentWaypoint]); // Child object always Looking in the direction of the current waypoint.
         
 transform.rotation.y = compass.rotation.y; // Set my rotaion on Y to match that object on Y.

This gives an error..

Also this makes me face theat rotaion in ALL axis, when i only want the Y axis to change.

 compass.LookAt(path.vectorPath[currentWaypoint]); // Child object always Looking in the direction of the current waypoint.
 transform.rotation = Quaternion.Lerp(transform.rotation, compass.rotation, Time.deltaTime * turnSpeed); // Then set rotation to that compass look roation


And this one, while almost waht i ant makes them walk backwards everywhere. But it dies however only rotate the one Axis Y.

 Vector3 faceThisWay = transform.position - (path.vectorPath[nextWaypointDistance]);
         faceThisWay.y = 0.0f;
         Quaternion newRotation = Quaternion.LookRotation(faceThisWay, transform.up);
         transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 20);

So gang, which one is right and where is it going so wrong.. Many Thanks..

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 HarshadK · Feb 26, 2015 at 11:47 AM

For the last method you should change your first line to:

 Vector3 faceThisWay = (path.vectorPath[nextWaypointDistance]) - transform.position;

And it should work.

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 sniper43 · Feb 26, 2015 at 11:56 AM 0
Share

Yes, this is a better solution than my answer, as it doesn't depend on euler angles.

avatar image p_unity · Feb 26, 2015 at 12:09 PM 0
Share

Yep that does work.. sorry i removed th comment that said it doesnt work i changed something else before posting .. The nextWaypoint should actually be currentWaypoint. different types hence the error. Thanks dudes

avatar image
1

Answer by sniper43 · Feb 26, 2015 at 11:54 AM

 compass.LookAt(path.vectorPath[currentWaypoint]); // Child object always Looking in the direction of the current waypoint.
 transform.rotation.y = compass.rotation.y;

The reason this throws an error is because transform.rotation.y is a getter and has no setter.

Modifying this to work would look like this:

 compass.LookAt(path.vectorPath[currentWaypoint]); // Child object always Looking in the direction of the current waypoint.
 transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, compass.rotation.y, transform.rotation.eulerAngles.z );

To lerp it I'd recommend Mathf.lerpAngle:

 float interpolator = 0.5f;
 compass.LookAt(path.vectorPath[currentWaypoint]); // Child object always Looking in the direction of the current waypoint.
 transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, Mathf.lerpAngle(transform.rotation.eulerAngles.x,compass.rotation.y, interpolator), transform.rotation.eulerAngles.z );

I will say this: Using the euler angles of object is easier to use, but can cause SO MANY ANNOYING PROBLEMS. So use carefully.

@HarshadK's answer is better as the rotation doesn't depend on euler angles.

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 p_unity · Feb 26, 2015 at 12:09 PM 0
Share

Appreciate the answer. I just the other easier to understand :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Camera following/looking at aircraft 1 Answer

child lookat another child 1 Answer

Turret Rotation Problems 1 Answer

"Look rotation viewing vector is zero" error 2 Answers

transform.up flips gameobject by it's x-axis when transform.up.x is 0 and when transform.up.y is negative 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