- Home /
Follow rotation of another object but at half the speed
My problem lies that i want the body to follow the rotation of my head, but at a lower speed. Here is my code.
public GameObject eyes;
public GameObject hips;
public float y;
public Quaternion rotatePos;
// Use this for initialization
void Start () {
eyes = GameObject.Find("CenterEyeAnchor");
hips = GameObject.Find("xbot(Clone)/mixamorig:Hips/mixamorig:Spine/mixamorig:Spine1/mixamorig:Spine2");
}
// Update is called once per frame
void Update () {
rotatePos = Quaternion.Euler(0, transform.eulerAngles.y * 0.5f, 0);
hips.transform.rotation = rotatePos;
transform.rotation = eyes.transform.rotation;
}
Now the transform.eulerAngles.y * 0.5f works fine if you go from 0 counting up towards 360 (going right), but as you might guessed if you count down from 0, 360, 359 358.. etc (going left), it automatically goes to 180 due to the calculation.
My problem is i do not know how to set the rotation speed at half of what my head is. Thanks in advance!
Answer by davidcox70 · Apr 03, 2018 at 02:43 PM
Hmmm.
Depending what exactly you need to achieve, would this work maybe?
hips.transform.rotation=Quaternion.Lerp(eyes.transform.rotation,hips,transform.rotation,0.5f);
Basically, the hips rotation would rotate to be half as rotated as the eyes. Over the same period of time, this would equate to rotating at half the speed whilst always relating to the original origin - i.e. when eyes are faced forward, so is the body.
DC
...on second thoughts;
Vector3 fixedForward=new Vector3(0,0,1); // a reference direction
hips.transform.rotation = Quaternion.LookRotation(Vector3.Lerp(fixedForward,eyes.transform.forward,0.5f));
This rotates the hips half as much as the eyes, with reference to the world's forward direction.
What i wanted to achieve was a way to control the body of an avatar accordingly to the head when in VR. Your helped worked perfectly thank you ! :)
Answer by mikewarren · Apr 03, 2018 at 07:00 PM
Your problem is that angles wrap at 360 degrees. I haven't used it, but Mathf.LerpAngle looks like what you might need.
Yeah $$anonymous$$athf.LerpAngle was exactly what was necessary. As $$anonymous$$cox70 posted above worked like a charm !
Your answer
Follow this Question
Related Questions
Reversing rotation back to zero 0 Answers
How to get Vive Controller Rotation? 0 Answers
Setting Rotations 2 Answers
What's wrong with my if states of eulerAxis.x in Update? 1 Answer
Change rotation of an object based on an HTC Vive Controller 1 Answer