0
\$\begingroup\$

I have a camera that rotates around an object to simulate the rotation of this object. To do this, I simply move my camera to the object before making my rotation and then translate it back:

void Update()
{
        distanceToTarget = Vector3.Distance(camera.transform.position, myObject.center);
        transform.position = myObject.center;

        transform.Rotate(Vector3.up, horizontalRotationValue);
        transform.Rotate(Vector3.right, verticalRotationValue);

        transform.Translate(new Vector3(0f, 0f, -distanceToTarget));

        centeredPosition = transform.localPosition;
        ...

My problem comes from the fact that I would like to also be able to have an offset of the view of my camera in relation to the object. All this while continuing to simulate the rotation of my object by rotating the camera around.

camera offset
After my current rotation logic, the object is still in the center of the view. So at the first iteration of my rotation, I save the difference between the centered position on the model and the offset camera.
offsetToCenter = transform.localPosition - centeredPosition;

So, I hope to be able to use this offset to shift my camera again after it has rotated. However, the recorded offset no longer corresponds to the offset on the local space of the camera since it has rotated.
transform.localPosition += transform.localRotation * offsetToCenterPosition;

The rotation works well but not the position. There is a step that I must be doing wrong or that is missing. I think it comes from the conversion of offsetToCenterPosition with the new rotation of my camera.

New contributor
RonanV is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
3
  • \$\begingroup\$ Did you try offsetToCenter = Quaternion.Inverse(transform.localRotation) * (transform.localPosition - centeredPosition); ? \$\endgroup\$
    – DMGregory
    Jun 20 at 14:36
  • \$\begingroup\$ This is indeed what I was missing ! Thanks a lot \$\endgroup\$
    – RonanV
    Jun 20 at 14:44
  • 1
    \$\begingroup\$ Want to write up your solution as an Answer below? \$\endgroup\$
    – DMGregory
    Jun 20 at 14:49

1 Answer 1

0
\$\begingroup\$

It was indeed coming from the offsetToCenterPosition calculation. Thank you DMGregory.

Here is the full answer:

void Update()
    {
        distanceToTarget = Vector3.Distance(camera.transform.position, myObject.center);

        transform.position = myObject.center;

        transform.Rotate(Vector3.up, horizontalRotationValue);
        transform.Rotate(Vector3.right, verticalRotationValue);

        transform.Translate(new Vector3(0f, 0f, -distanceToTarget));

        centeredPosition = transform.localPosition;

        transform.localPosition += transform.localRotation * offsetToCenterPosition;
    }

void UpdateOffsetToCenterPosition() //called on first iteration
    {
        offsetToCenterPosition = Quaternion.Inverse(transform.localRotation) * (transform.localPosition - centeredPosition);
    }
New contributor
RonanV is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .