Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by frogsfordinner · Sep 24, 2021 at 10:51 PM · rotationrotatechilddirectionlimit

Limiting RotateTowards on two axes

Hello,


I'm trying set the rotation of a child transform to that of a vector pointing in the direction of a target transform, with limits on the min/max rotation on each axis (X and Y).


Currently I'm using the code below to both limit the rotation of the target direction vector and set the rotation of the child transform:


Vector3 targetVector = Camera.main.transform.position - transform.parent.position; Vector3 rotateVector = Vector3.RotateTowards (transform.parent.forward, targetVector, 50f * Mathf.Deg2Rad, 0f); transform.rotation = Quaternion.LookRotation (rotateVector, transform.parent.up);

While this works and limits the max rotation of the child transform to fifty degrees on both the X and Y axis ideally I would like to be able to set a minimum and maximimum rotation value for each axis separately.


Using the parent transforms forward vector as zero degrees and referencing the parent transform up vector and the direction to the target vector:

  1. I'd like to be able to limit the child transforms rotation on the X axis to a minimum of negative forty five degrees to a maximum of seventy degrees.

  2. I'd like to be able to limit the child transforms rotation on the Y axis to a minimum of negative forty degrees to a maximum of ten degrees.


    I've created two GIFs to hopefully better explain my intentions, the first GIF from a side view and the second GIF giving a top down view:


    example a example b

    In both GIFs the coloured lines and arc segments represent:


  • The pink cross is the target transform.

  • The orange line is the direction to the target transform vector.

  • The solid blue line is the forward vector of the child transform.

  • The solid green line is the up vector of the child transform.

  • The dashed blue line is the forward vector of the parent transform.

  • The dashed green line is the up vector of the parent transform.

  • The brighter green arc represents the ideal min/max rotation of the target vector.

  • The darker green arc represents the current limited rotation of the target vector.

  • The brighter red arc represents the ideal min/max rotation of the target vector.

  • The darker red arc represents the current limited rotation of the target vector.

    I'd like to preemptively apologize for the word mess above (easily the worse text editor I've ever used) and say any help would be greatly appreciated, thank you.

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
0

Answer by Tom_Bombardir · Sep 25, 2021 at 09:50 AM

Not sure if that’s what you need, but you can try Mathf.Clamp. It limits the value going beyond a certain point with minValue and maxValue

https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html

Comment
Add comment · Show 3 · 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 frogsfordinner · Sep 25, 2021 at 02:25 PM 0
Share

I've tried to clamp serveral different values but I don't think I understand enough about euler/quaternions to make any real progress, thank you though.

avatar image Tom_Bombardir frogsfordinner · Sep 25, 2021 at 04:16 PM 0
Share
 transform.rotation = Quaternion.LookRotation (new Vector3 (Mathf.Clamp(rotateVector.x, $$anonymous$$XDegree, maxXDegree), Mathf.Clamp(rotateVector.y, $$anonymous$$YDegree, maxYDegree), rotateVector.z), transform.parent.up);

Again, not sure if this will work and if this is what you need, but you can try

avatar image frogsfordinner Tom_Bombardir · Sep 25, 2021 at 06:02 PM 0
Share

The value RotateTowards returns seem to be a Vector3 with each axis ranging between negative one, and positive one (though rarely going higher than naught point one). I've tried multiple combinations of positive and negative, large and small values for the $$anonymous$$/max degrees but no luck so far.

Vectors and quaternions remain a mystery to me, thank you once again though.

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

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

Limiting rotation of object, specifically using scroll wheel 2 Answers

Set rotation based on 2 points problem 1 Answer

Is it possible to keep an object upright inside another game object that is spinning or rolling 1 Answer

Limit GUI Rotation? 2 Answers

Rotate but keep direction 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