- Home /
Rotation around an Object
Hello all , I am having a hard time understanding what this code does : (it was taken from a tutorial but the explication wasn't clear enough to me) I came to a point where I need this code but with a minor change . I can't change it since I don't understand it well. The goal behind all of it is to make an object rotate around a certain pivot . This object had a predefined position and rotation. The problem with the code as it is is that the object will literally have a big jump from one position and rotation to a completely new position and rotation once the code is activated. It should not jump like that but have a smooth entrance to this code. If you can offer me any kind of help , i'll be really grateful. Thanks for all in advance ! Kindly find the code down below:
//Horz and Vert are random values between -1f and 1f Rotx += Horz Time.deltaTime RotSpeed; Roty += Vert Time.deltaTime RotSpeed; Quaternion Yrot = Quaternion.Euler(0f, Roty, 0f); DestRotation = Yrot Quaternion.Euler(Rotx, 0f, 0f); DestRotation = Quaternion.Euler(Rotx, Roty, 0f); transform.rotation = DestRotation; transform.position = Pivot.position + transform.rotation Vector3.forward * -PivotDistance;
Answer by UltraSage · Jun 01, 2017 at 05:46 AM
I'll try to break this code down for you:
//Horz and Vert are random values between -1f and 1f
Rotx += Horz*Time.deltaTime*RotSpeed;
//Given Horz is a random number, Rotx is assigned a slowed down RotSpeed value
Roty += Vert*Time.deltaTime*RotSpeed;
//Just like above, but with Vert/Roty variables
Quaternion Yrot = Quaternion.Euler(0f, Roty, 0f);
//A Quaternion Yrot is declared and assigned Roty value for rotation in y axis
DestRotation = Yrot*Quaternion.Euler(Rotx, 0f, 0f);
//Assign multiplied Quaternions to DestRotation. I'm not sure of the outcome but I'm guessing this would return 0, hence Yrot is (0,X,0) and the other quaterion is (X,0,0) and X*0 = 0. This line seems completely useless
DestRotation = Quaternion.Euler(Rotx, Roty, 0f);
//This is essentially assigning the quaternion with Rotx and Roty correctly to DestRotation
transform.rotation = DestRotation;
//Rotation of DestRotation is assigned to bearer of the script
transform.position = Pivot.position + transform.rotation*Vector3.forward * -PivotDistance;
//The position of the bearer of the script is changed after quasi trigonometric calculation
The code is sluggish and complicates the issue. If you simply want to rotate object around some given point, you might want to check out RotateAround() function. With it your script would look like this (In the update function):
transform.RotateAround(Pivot.position,Vector3.up,10f*Time.deltaTime);
while: Vector3.up is indicating that the rotation will take place around Y axis. Vector3.forward would be turning around Z axis, and Vector3.right would turn around X axis. and 10f*Time.deltaTime is just a placeholder value, you can set any value you want there. Time.deltaTime helps you to keep rotation consistent with game's framerate.
Your answer
Follow this Question
Related Questions
Get rotation around (forward) axis ? 1 Answer
Rotate Rigidbody to always face a force 0 Answers
Problem with normal snapping and rotations. 1 Answer