【问题标题】:Unity Roll-A-Ball Main CameraUnity Roll-A-Ball 主摄像头
【发布时间】:2016-05-06 20:45:32
【问题描述】:

我无法让我的相机位置随播放器移动。

这是 CameraController.cs

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour
{

    public GameObject Player;
    private Vector3 offset;
    void Start()
    {
        transform.position = Player.transform.position;
    }

    void LateUpdate()
    {
        transform.position = Player.transform.position;
        Debug.LogError(transform.position);
    }
}

脚本是主摄像机的一个组件。相机不是玩家对象的子对象,反之亦然。

调试表明位置正在更新到玩家的位置,但是当游戏运行时,相机是静止的,不会从初始起点移动。

【问题讨论】:

  • 这看起来应该可以。非常奇怪的是,摄像机位置的调试显示它与玩家的位置相同,如果它没有明显变化的话。我认为您的错误必须在您的会话中的其他地方。您的启动功能和成员变量offset 的减速是不必要的,但这对您要实现的目标没有任何影响。您的会话中是否还有其他脚本可以更改相机的位置?您是否在控制台中遇到任何错误?

标签: c# unity3d 3d


【解决方案1】:

试试这个:

using UnityEngine;
using System.Collections;

public class CameraController: MonoBehaviour {

public GameObject Player;
private Vector3 offset;

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

void LateUpdate () {
    transform.position = Player.transform.position + offset;
    }
}

偏移量是相机和播放器之间的距离。

另一种方法是让相机成为玩家的孩子。

【讨论】:

  • 实际上你不能让相机成为玩家的孩子,因为玩家滚动会导致整个相机旋转。事实上,教程显示了这一点。
  • 但是如果您根据方向冻结某些轴,它将起作用
【解决方案2】:
using UnityEngine;
using System.Collections;

public class CameraFollower : MonoBehaviour
{
    public Transform thePlayer;

    private Vector3 offset;

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

    void Update()
    {
        Vector3 cameraNewPosition = new Vector3(thePlayer.position.x + offset.x, offset.y, thePlayer.position.z + offset.z);
        transform.position = cameraNewPosition;
    }
}

【讨论】:

    【解决方案3】:

    非常感谢您发帖并提供帮助。问题是我试图让脚本在支持虚拟现实的环境中移动相机。我发现,在 VR 环境中,相机的行为方式以及随后相机的移动方式是不同的,并且相机需要是要移动的对象的子对象,并且不能通过脚本移动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2023-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多