【发布时间】:2018-11-05 09:55:49
【问题描述】:
您好,我正试图让我的相机在我的玩家 (Sphere) 移动时跟随它。我已经让相机跟随玩家,但是当球体旋转时,相机也会旋转。那不是我要找的。我需要相机保持在球体上并在我转动时旋转。到目前为止,这是我的代码:
编辑:我正在寻找的是你在视频中看到的东西。当球体转动时相机旋转的地方,所以它一直在它后面。 https://www.youtube.com/watch?v=jPAgPQi1l0c
using UnityEngine;
using System.Collections;
public class TransformFollower : MonoBehaviour
{
[SerializeField]
private Transform target;
[SerializeField]
private Vector3 offsetPosition;
[SerializeField]
private Space offsetPositionSpace = Space.Self;
[SerializeField]
private bool lookAt = true;
private void Start()
{
offsetPosition = new Vector3(-3, -2, 0);
}
private void Update()
{
Refresh();
}
public void Refresh()
{
if (target == null)
{
Debug.LogWarning("Missing target ref !", this);
return;
}
// compute position
if (offsetPositionSpace == Space.Self)
{
transform.position = target.TransformPoint(offsetPosition);
}
else
{
transform.position = target.position + offsetPosition;
}
// compute rotation
if (lookAt)
{
transform.LookAt(target);
}
else
{
transform.rotation = target.rotation;
}
}
}
【问题讨论】:
-
相机是玩家的孩子吗?
-
不是,是对象。
-
您可以尝试将其设为播放器的子级。当然是在层次结构中。
标签: unity3d