简体   繁体   中英

How to make camera repeat rotation and position of another camera?

How can I make a camera in Unity, that repeats the rotation and position of another camera on all three axes?

I'm thinking about portals - how to repeat players camera movement and rotation by another camera which renders texture for the portal, to create a realistic effect while player moves - that there is a whole new scene behind the portal.

Imagine I have a player camera and another camera in another place in the scene. The second camera may have different position and rotation initially. But when the player camera rotates 90 degrees to the left, the second camera should add +90 degrees to the left to its current rotation.

And the same with movement, so if the player moves 1 meter forwards, the camera moves 1 meter forwards from its current position.

You can create a script that takes a transform as external input which copies the values from one object to the other. If you want to keep the offset, so just look and move in the same direction, but not be at the same location that is also possible.

The following script lets you mimic another object:

public class Mimic : MonoBehaviour
{
    [SerializeField]
    private Transform other;
    private Vector3 offset;

    private void Start()
    {
        offset = transform.position - other.position;
    }

    private void Update()
    {
        transform.rotation = other.rotation;
        transform.position = other.position + offset;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM