- Home /
Rotate One Object by Another, without matching the rotation?
Been trying to work this one out all day, I’m trying to make object A’s rotation affect Objects B’s rotation but without having Object A and Object B to match rotations.
At first I thought, just add the rotation value from A to B, but then noticed it's not that simple ;).
There are two major problems I can see, one: values need to mirror to reverse the rotation direction and two: the rotation value has to be some sort of usable offset, something that is also taking the original rotation into account.
I know there must be some kind of magic function to calculate this but really cant find it.
Been digging through docs and posts all day with no joy.
I'm not sure what you're asking. On Object B, you can change its rotation by transform.Rotate . With that in script, you could rotate it by whatever you want, including the value of Object A's rotation.... but I'm not sure what you're asking
I'm trying to transfer the rotation of one object to the other but only using the amount of rotation applied. Also to do this in a full 360.
I will play some more with "transform.Rotate" maybe there is something I’m missing. Possibly calculate the rotation needed between a $$anonymous$$ and max value.
What do you mean by transfer rotation but only using the amount of rotation?
Say object A has a rotation of 80 degrees, Object B has a rotation of 10 degrees. What do you want to happen?
ok just so we are on the same page here:
this.transform.Rotate(this.transform.rotation.x, CTRL_PIVOT.rotation.y, this.transform.rotation.z * Time.deltaTime);
is not the answer.
What I'm trying to do in simple terms is apply the amount of rotation from object A to object B. in other words if object A is facing forwards and object B is facing backwards, and then object A is rotated lets say 30 degrees then Object B will also rotate in the correct 30 degrees.
However what I'm really after here is for both objects to be at completely random orientations but still abide by the same rules. When object A is rotated then object B rotates but not having to match the rotations, for example not ObjectA.rotation = ObjectB.rotation.
Sounds simple!
ok think I may have just found the solution, will post it if works.
Answer by robertbu · Aug 05, 2013 at 08:31 PM
Answer rotations questions like this one can be tricky since words can be visualized different ways. Here is what I think you are asking for:
#pragma strict
var target : Transform;
private var offset : Quaternion;
function Start() {
offset = Quaternion.Inverse(target.rotation) * transform.rotation;
}
function Update() {
transform.rotation = offset * target.rotation;
}
Assu$$anonymous$$g you want to match all three axes, the above code appears to match your video.
thanks for that will test it out later. I knew it would be something simple.
Answer by superme2012 · Aug 06, 2013 at 12:13 AM
It's 1:03 AM here and finally got it solved.
I did a hybrid between the code you showed and the TransformConstraint script, I used this to flip the constraints calculations:
this.transform.rotation = Quaternion.Inverse(target.rotation);
works like a charm, and allows to control all or just one axis at a time.
Now for some sleep ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613111606im_/https://answers.unity.com/themes/thub/images/avi.jpg)