Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Andy 1 · Mar 08, 2010 at 06:26 PM · triggerquaternion

Trigger on reaching target rotation only working the first few times.

I have an object smoothly rotating using Quaternion.Slerp towards a variable called 'target', which is set by a Quaternion.LookRotation.

I have set an if statement to trigger an action when I reach the target rotation

if (transform.rotation == rotate)

This works for the first several movements, but then only works intermittently. Does anyone have any ideas on what this might be?

I thought there might be a rounding error, meaning it never reaches the actual value, so tried accepting a value that was near, but Quaternion values will not accept a plus or minus, so I cannot do if (transform.rotation - rotate > 3 && transform.rotation - rotate < -3) for 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

1 Reply

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

Answer by duck · Mar 08, 2010 at 07:58 PM

You're probably running into problems to do with imprecision when comparing floating point values.

You could use Quaternion.Angle to check whether the difference between the current angle and the target angle is below a given threshold. For example:

if (Quaternion.Angle(transform.rotation, rotate) < 0.1f) { 
   // we're within 0.1 degrees of the target... close enough!
}

Alternatively, if you're actually linearly Slerping towards a target (that is, feeding in a start and end rotation, as opposed to feeding in the current and end rotation, then you would be able to tell whether the Slerp has reached the end angle when the value for the 3rd argument of Slerp is greater than or equal to 1.

Eg:

if (rotationFactor <= 1) {
    transform.rotation = Quaternion.Slerp(startRot, endRot, rotationFactor)
} else {
    // we have reached the end rotation
} 
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 Andy 1 · Mar 08, 2010 at 09:18 PM 0
Share

Thank you. Your Quaternion.Angle code was exactly what I wanted. It solved the problem, but I had to remove the f after the 0.1. I upped the number to 1 and it made it slightly more responsive, as if it was taking a few frames to complete the last degree.

avatar image duck ♦♦ · Mar 08, 2010 at 09:54 PM 0
Share

Ah, yes the 'f' is C# syntax required when writing a floating point value. If your writing in JS, you don't need it.

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

Can't click gameobject when over another trigger? 1 Answer

Object rotate to target in 2D? Not working properly. 0 Answers

Quaterion is rotating my character improperly? 1 Answer

how to make a simple full rotational quaternion for cameras 3 Answers

How to initiated floors to fall like dominos once the game starts 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