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 KaiseanGames · Feb 17, 2011 at 06:27 PM · rotationlookatslerp

Incorrect script for slerp rotation

Take a look at this video for a visual description of my problem: http://www.youtube.com/watch?v=Sz_vYC7VdRE

The script chooses the closest cube and then rotates the turret dome towards the selected cube. On the video you can see me clicking on the cube (removing the cube) so the turret will turn towards another cube.

Note that the rotation is limited to the y-axis. The thing is - there are two possible ways to rotate towards an object - the shorter and the longer (except if you are facing opposite direction in which case the left turn and the right turn are the same length). As you can see in the video, the turret choses the wrong (longer) turn when only one cube remains and I can't figure out why.

Here is the code. I am a total newb with these transformations so this is probably the bad way to do such a thing (obviously) - but what I'd like to know is what needs to be changed in my code so that the turning will still be constrained to y-axis but the rotation will happen always the shorter way?

EDIT (with code updated): I have found out that it happens because the destination vector3 is (0,0,0) so slerping from (0,x,0) to (0,0,0) interpolates from x to 0, not taking in account the periodicity of the angles. But I wish there's a simple way to rotate towards the object by rotating the smallest possible angle.

Box closest = FindClosestBox();

if (closest != null) { Vector3 previousRotation = new Vector3(TurretDome.eulerAngles.x, TurretDome.eulerAngles.y, TurretDome.eulerAngles.z); TurretDome.LookAt(closest.transform); Vector3 destinationRotation = new Vector3(0, TurretDome.eulerAngles.y, 0); Vector3 stepRotation = Vector3.Lerp(previousRotation, destinationRotation, RotationSpeed * Time.deltaTime); TurretDome.eulerAngles = stepRotation; }

Thanks in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Mike 3 · Feb 17, 2011 at 07:40 PM

You should be using Quaternion.Slerp or Quaternion.RotateTowards instead of Vector3.Lerp/Slerp/AnythingVectory

Vector3 offset = closest.transform - transform; offset.y = 0; //clamp it to y rotation only Quaternion lookAt = Quaternion.LookRotation(offset);

transform.rotation = Quaternion.RotateTowards(transform.rotation, lookAt, RotationSpeed * Time.deltaTime);

//OR

transform.rotation = Quaternion.Slerp(transform.rotation, lookAt, RotationSpeed * Time.deltaTime);

RotateTowards will go at a fixed speed in degrees per second, Slerp will be a fairly exponential decay movement

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 KaiseanGames · Feb 17, 2011 at 07:45 PM 0
Share

Just a few $$anonymous$$utes before your answer, I stumbled upon a solution on some Youtube tutorial. Thanks anyway. It had to be something simple :)

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

No one has followed this question yet.

Related Questions

How to make a gameobject look at(rotate) another gameobject smoothly? 0 Answers

How to make an object look at another object over time? 2 Answers

Look at rotation at a moving object while moving (C#)(2D) 3 Answers

Get slerp to work just as LookAt(,Vector3.right) does 1 Answer

How do I swap two objects without them colliding ? 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