- Home /
The question is answered, right answer was accepted
Rotate object instead of camera around it?
How to rotate an object as if it was seen by another camera?
I have a GameObject and a main camera seeing it from (0, 0, -1). I positionned several cameras (views) around the object to configure different views of it, but for design reasons*, I can't move the main camera (the others are just useful for their preview in edit mode). In addition, I don't know at advance the initial rotation of the model, then I may use only delta rotations. So how do I rotate the object as if it was seen from the other cameras?
I've tried this :
private void RotateModel(Transform fromView, Transform toView)
{
// Rotate the object by using a delta angle from the current point of view to the next
// Calculate delta
Vector3 vecFrom = (fromView.localPosition - model.localPosition).normalized;
Vector3 vecTo = (toView.localPosition - model.localPosition).normalized;
Quaternion delta = Quaternion.FromToRotation(-vecFrom, -vecTo);
// Rotate the object
Quaternion newModelRotation = model.localRotation * delta;
HOTween.To(model, 1.25f, new TweenParms().Prop("localRotation", newModelRotation));
//model.localRotation = newModelRotation;
}
But I always get wrong results... I tried not to negate the vectors, change their order, calculate delta with Quaternions instead but no success... it's like I missed something but don't know what. Any ideas?
could rotate the camera around anyway, but in my game I would have to create a new layer for each object, that would waste a lot of them as there is lots of objects to watch...
This is totally, completely wrong - heh!
You'll be very pleased to know it's actually super-easy to do this.
Just use
http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html
Footnote, "RotateAround" is often very useful, too ... http://docs.unity3d.com/Documentation/ScriptReference/Transform.RotateAround.html
I know these functions, but the problem isn't just how to rotate an object, but doing it the way it behaves as a Google-earth-like view : I could place a pivot on its center, place the camera as child of it and rotate the pivot to orbit around. However, I'm trying to achieve the same behaviour, but without moving the camera, and rotating the object ins$$anonymous$$d.
Finally, I used the first option and reduced the far plane of each camera so they don't see each others...