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 Arctic_Fletcher · Dec 17, 2017 at 10:46 AM · rotationrandomrange

Random Rotation With Limited Range?

I'm having a lot of difficulty phrasing this question, so please bear with me. I've got an object that I want to rotate at random without rotating too far in any direction in relation to its parent object. In this case, it's an eyeball, with the parent object being its socket. If the socket is rotated, I'd like the eye to rotate with it, while also rotating independently within a certain range relative to the socket to "look around," if that makes any sense. But, I've run into a problem. I've got the eye rotating randomly, but if I rotate the socket, the eye keeps facing the same general direction, without rotating with the socket. Again, I'm sorry if this makes little sense. I've been sitting here trying to think of a way to adequately explain this problem for nearly an hour, now.

The relevant code is below. It's a modified version of a script to make the eye track the player. Any help would be greatly appreciated.

 private Quaternion targetRotation;    
 
 void Update () {
             targetRotation = Quaternion.Euler(Random.Range(-35, 35), Random.Range(145, 215), 0);
             transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * 15.0f);
         }


Edit: I've added a video that, hopefully, helps better explain my problem. https://youtu.be/tWlQSLOO0yI

Comment
Add comment · Show 2
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 Yeezyy · Dec 19, 2017 at 02:56 AM 1
Share

Not sure if it works try switching transform.rotation to transform.localRotation

avatar image Arctic_Fletcher Yeezyy · Dec 19, 2017 at 06:06 AM 0
Share

This worked. Thank you so much! I'd accept it as the correct answer if I could.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by BrayanKrad03 · May 10, 2018 at 07:28 AM

use localrotation instead of rotation, that is a global rotation, hope it helps you, greetings.

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
avatar image
0

Answer by abhaychandna · Dec 17, 2017 at 11:30 AM

I didn't understand your question fully but If I understand your question correctly then what you need to do is have 2 gameobjects - socket and eyeball - eyeball is the child of socket. So now if you rotate the socket through whatever means , your eyeball will also rotate with it. Now comes the eyeball random rotation. For that what you can do is

  //Write this code in fixedUpdate
     xLeft = socket.transform.position.x - xLengthOfSocket;
     xRight = socket.transform.position.x + xLengthOfSocket;
     yDown = socket.transform.position.y - yLengthOfSocket;
     yUp = socket.transform.position.y + yLengthOfSocket;
    eyeball.rotation = Quaternion.Euler(Random.Range(xLeft,xRight),Random.Range(yDown,yUp),0);
 // x length of socket is probably 35 and if y lengths are 2 different lengths then you can also use 2 
 //different values in place of yLengthOfSocket
 //If this code doesn't work Mathf.Clamp might work to keep the eyeball inside the socket.     


Comment
Add comment · Show 8 · 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 Arctic_Fletcher · Dec 17, 2017 at 08:30 PM 0
Share

Once again, the random rotation works, and keeps it within a certain range, but when the parent object is rotated, the eye doesn't rotate with it. Ins$$anonymous$$d, it just faces directly down the Z axis, regardless of the socket's rotation.

avatar image abhaychandna Arctic_Fletcher · Dec 18, 2017 at 03:17 AM 0
Share

Add in this code after the eyeball.rotation = Quaternion . Euler statement

 eyeball.Rotation = 
 Quaternion.Euler($$anonymous$$athf.clamp(xLeft,xRight),$$anonymous$$athf.Clamp(yDown,yUP),$$anonymous$$athf.Clamp(0,0));

This will fix the z axis of the eyeball.rotation to 0 as u don't want that to move and the x and y position of the eye will never go beyond the socket's x position and y position. If this doesn't work, Could you tell the values of socket.trans.positon.y and position.y and x and y lengths of the eye and what are the values of x,y and z during runtime after this code works.

avatar image Arctic_Fletcher abhaychandna · Dec 18, 2017 at 03:59 AM 0
Share

Doesn't $$anonymous$$athf.Clamp require three floats, though? Here it only has two, and as a result, it's giving me compiler errors.

Show more comments

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

105 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

Related Questions

How can i add random range to an instantiated objects rotation? 1 Answer

Random rotation 2 Answers

Flip over an object (smooth transition) 3 Answers

How to know what random number is chosen 2 Answers

Random Range Seems... Unrandom 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