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 /
  • Help Room /
avatar image
0
Question by · Sep 22, 2017 at 06:23 PM · rotationtransformquaternion

Clamping rotation between two angles

I'm working on kind of an infinite runner type game. I have seen this answer: http://answers.unity3d.com/questions/1409655/how-to-clamp-rotation-between-negative-and-positiv.html but it does not work for me (the car rotates too far to the right and does not rotate at all to the left).

I'm trying to keep an object that is chasing the player within 7 degrees of 0 (so minimum Euler is (0, -7,0) and max is (0, 7, 0)). It only needs to rotate on the Y axis. Basically, when it is moving left to follow the player I want it to quickly approach the left angle (-7) and vice versa for the right instead of just snapping to +/- 7.

I've tried using Quaternion.Lerp and Slerp but they had the same issue as the answer I found so I'm thinking something could be wrong with my angle math.

To keep things simple in my code I have a function that is passed a float. If the float is 0 it just sets the rotation to Quaternion.identity, otherwise it (is supposed to) turn the object according to whether or not the float is positive or negative.

This is my function:

     //MaxAngleY is 7, turnAmt is either 0 or +/- ~500 (sidespeed is 500)
     public void AngleBasedOnTurn(float turnAmt)
     {
         float turnSpeed = 5f;
         if (turnAmt == 0)
         {
             transform.rotation = Quaternion.identity;
             return;
         }
         //amt is always going to be 7 or -7, it is correctly showing up as -7 when car is turning to left
         float amt = MaxAngleY * (turnAmt / SideSpeed);
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, amt, 0), Time.deltaTime * turnSpeed);
 
    }

Quaternion.Lerp has had the best results. The problem is it works fine to angle the car to one side but then has problems angling it back to the other side. I have a quick video demonstrating this here: https://www.youtube.com/watch?v=K0NPXUV8JUY

What am I doing wrong?

Comment
Add comment · Show 5
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 Glurth · Sep 22, 2017 at 06:29 PM 0
Share

You don't show us how you calculate turnAmt. if turn amount is always == to SideSpeed: then (turnAmt / SideSpeed); will always be +1, and never be -1.

avatar image Glurth · Sep 22, 2017 at 07:46 PM 0
Share

turnAmt can be negative (that's poor phrasing on my part), so turnAmt/SideSpeed can be +1 or -1. turnAmt is just the sideways force applied to the rigidbody.

amt is being calculated correctly, I verified with some Debug.Log's

avatar image Glurth · Sep 22, 2017 at 08:15 PM 0
Share

I'm trying to keep an object that is chasing the player within 7 degrees of 0 (so $$anonymous$$imum Euler is (0, -7,0) and max is (0, 7, 0)).

I suspect this concept is the issue. What if the target is outside of that 14 degree arc?

$$anonymous$$ay I suggest you ins$$anonymous$$d apply a $$anonymous$$axTurnRate ins$$anonymous$$d:

 Quaternion $$anonymous$$axTurnRate = Quaternion.Euler(0, 7, 0); //number of degrees per sec
 float oneOr$$anonymous$$inusOne = (turnAmt / SideSpeed);
 transform.rotation *= (oneOr$$anonymous$$inusOne * Time.deltaTime) * $$anonymous$$axTurnRate;  //multiplying two quaterions effectively applies one rotatation to another (adds em).. 


avatar image Glurth · Sep 23, 2017 at 01:51 AM 0
Share

That has a more desirable effect but it's still turning pretty far. I changed some things up in the code because you brought up a good point about being outside of range. I now froze rotation completely and the function now unfreezes Y rotation and then freezes it again at the end to get rid of any external factors.

I think what I'm going to do (since I only need rotation on one axis and other functions are designed to work with all 3) is manually increase or decrease a float and keep that float on the range of [-7, 7] and then set the Euler Y to the float and see how that goes.

avatar image Glurth · Sep 23, 2017 at 02:07 AM 0
Share

Yup, that worked, thanks for pointing me in the right direction!

0 Replies

· Add your reply
  • Sort: 

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

110 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

Related Questions

How to use Quaternion.Slerp with transform.LookAt? 3 Answers

3 Axis Camera Rotation 0 Answers

Questions about Rotations in unity 1 Answer

Quaternion.RotateTowards for camera positions is buggy 1 Answer

Transform.Rotate with dynamic values 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