UNITY中的摄像头位置

时间:2018-01-20 17:21:59

标签: unity3d camera unity5 game-engine game-physics

我正在制作一个游戏(2D),其中一个物体以速度运行并跳到即将到来的平台上,我已经将相机作为游戏对象的孩子(即主要玩家)。问题是,每当我的游戏对象在撞击平台或障碍物时旋转,主摄像机也开始旋转。我无法解决这个问题,任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

您可以在Inspector中锁定Camera的旋转,也可以创建一个Camerascript来跟随玩家manuell。 Seconed更好,因为你可以很容易地添加平滑,死区或相机效果,例如拍到相机上的抖动。

统一页面中脚本的示例:

using UnityEngine;
using System.Collections;

public class CompleteCameraController : MonoBehaviour 
{

    public GameObject player;       //Public variable to store a reference to the player game object


    private Vector3 offset;         //Private variable to store the offset distance between the player and camera

    // Use this for initialization
    void Start () 
    {
         //Calculate and store the offset value by getting the distance between the player's position and camera's position.
         offset = transform.position - player.transform.position;
    }

    // LateUpdate is called after Update each frame
     void LateUpdate () 
    {
         // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
         transform.position = player.transform.position + offset;
    }

}

将此脚本添加到您的MainCamera中。然后将您的PlayerObject拖入相机Inspector中的Field“player”。

如果您需要进一步的帮助,请观看此视频here