- Home /
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
Not sure if it works try switching transform.rotation to transform.localRotation
This worked. Thank you so much! I'd accept it as the correct answer if I could.
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.
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.
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.
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.
Doesn't $$anonymous$$athf.Clamp require three floats, though? Here it only has two, and as a result, it's giving me compiler errors.
Your answer
Follow this Question
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