C# 统一滚球主摄像机
我无法使我的相机的位置与播放器一起移动 这是CameraController.csC# 统一滚球主摄像机,c#,unity3d,3d,C#,Unity3d,3d,我无法使我的相机的位置与播放器一起移动 这是CameraController.cs using UnityEngine; using System.Collections; public class CameraController : MonoBehaviour { public GameObject Player; private Vector3 offset; void Start() { transform.position = Pla
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);
}
}
脚本是主摄影机的一个组件。摄影机不是播放器对象的子对象,反之亦然
调试显示位置正在更新为玩家的位置,但当游戏运行时,摄像头是静态的,不会从初始起点移动。尝试以下操作:
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;
}
}
偏移量是相机和播放器之间的距离
另一种方法是让相机成为玩家的孩子。非常感谢您的发帖和帮助。问题是,我试图让脚本在支持虚拟现实的环境中移动相机。我发现,在虚拟现实环境中,相机的行为方式以及随后的移动方式是不同的,相机需要是要移动的对象的子对象,并且不能通过脚本移动。这看起来应该可以工作。非常奇怪的是,对摄像机位置的调试显示,如果摄像机的位置没有明显变化,它与玩家的位置是一样的。我想你的错误一定在你的课程中的其他地方。您的启动函数和成员变量
offset
的减速是不必要的,但这不会对您试图实现的目标产生任何影响。您的会话中是否有其他脚本可以更改相机的位置?你在游戏机上有什么错误吗?实际上你不能让相机成为玩家的孩子,因为玩家滚动,它会导致整个相机旋转。事实上,本教程显示了这一点。但如果根据方向冻结某些轴,它将起作用
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;
}
}