简体   繁体   English

如何使相机重复旋转和定位另一台相机?

[英]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? 如何在Unity中制作一个摄像机,该摄像机在所有三个轴上重复另一个摄像机的旋转和位置?

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. 但是,当播放器摄像头向左旋转90度时,第二个摄像头应将其当前旋转向左添加+90度。

And the same with movement, so if the player moves 1 meter forwards, the camera moves 1 meter forwards from its current position. 动作也一样,因此如果播放器向前移动1米,则摄像头将从当前位置向前移动1米。

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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