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 Saif_youssef89 · Apr 22, 2019 at 06:19 PM · rotationvector3quaternionslerplookrotation

Slerp to make the right/left side face another object

I'm trying to make the right or left side of an object look at another object, unlike the common approach where you would want the front of an object to face another object. But the code below instantly snaps the object to the new rotation, and I've been unable to add a gradual rotation (Slerp)

 Vector3 direction = (player.position - transform.position).normalized;
 transform.right = direction;

Would someone please explain how to add a Slerp in this example?

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

Answer by Magso · Apr 22, 2019 at 08:52 PM

 public float damping;
 Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);

This is took from the old standard assets SmoothLookAt

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 Saif_youssef89 · Apr 22, 2019 at 09:35 PM 0
Share

Yes, but what if i want to rotate so that my Left side is facing the other object.

avatar image Magso Saif_youssef89 · Apr 22, 2019 at 09:40 PM 0
Share

$$anonymous$$ake both sides Transform variables and reference them like so

 Quaternion leftSideRotation = Quaternion.LookRotation(target.position - leftSide.position);
 Quaternion rightSideRotation = Quaternion.LookRotation(target.position - rightSide.position);
 leftSide.rotation = Quaternion.Slerp(leftSide.rotation, leftSideRotation, Time.deltaTime * damping);
 rightSide.rotation = Quaternion.Slerp(rightSide.rotation, rightSideRotation, Time.deltaTime * damping);
avatar image
0

Answer by highpockets · Apr 22, 2019 at 10:35 PM

You should never set the directional vectors of a transform, you will get very strange results sometimes because setting transform.right does not necessarily mean that transform.forward or transform.up will end up where you want them.


You should get the axis to rotate first (if transform.right is exactly 180 degrees away from direction, you will have to rotate just a smidge for this to work) :


 if(-transform.right == direction){
 
 transform.Rotate(0, 0.1f, 0); //rotate a smidge on the y axis to get less than 180 degrees
 }

 Vector3 axis = Vector3.Cross( transform.right, direction );


Now get your angle:


 float angle =  Mathf.Sqrt(Vector3.Dot(direction, direction) * Vector3.Dot(transform.right, transform.right)) + Vector3.Dot(direction, transform.right);


Now create your quaternion:


 Quaternion newRot = new Quaternion(axis.x, axis.y, axis.z, angle).normalized;


Now we get our target rotation based on this new rotation we need to apply to our existing rotation:


 Quaternion targetRotation = newRot * transform.rotation;


Now apply that to slerp:


 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, timer);
 timer = timer + Time.deltaTime;


That should work for you.

Cheers

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

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

160 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 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 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 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

LookRotation Vector3 is Zero, Yet Slerp Still Rotates? 2 Answers

Rotating about and only one axis 1 Answer

Need help with rotation 4 Answers

Basic AI Locked Axis 1 Answer

What am I doing wrong with these rotations? 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