要保持相机相对于游戏对象的左半部分,可以使用以下代码示例来实现:
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target; // 游戏对象的Transform组件
public float distance = 10f; // 相机与目标的距离
public float height = 5f; // 相机与目标的高度
public float smoothSpeed = 0.5f; // 相机跟随平滑度
private void LateUpdate()
{
// 计算出相机目标位置左侧的位置
Vector3 targetPosition = target.position + target.right * -distance + target.up * height;
// 使用Lerp函数平滑地将相机位置移动到目标位置
transform.position = Vector3.Lerp(transform.position, targetPosition, smoothSpeed * Time.deltaTime);
// 相机朝向目标
transform.LookAt(target);
}
}
在这个代码示例中,我们将相机的位置设置为目标位置的左侧,然后使用Lerp
函数来平滑移动相机到目标位置。Lerp
函数的第一个参数是当前相机的位置,第二个参数是目标位置,第三个参数是移动速度。最后,我们使用LookAt
函数将相机朝向目标。
请注意,这是一个简单的示例,可能需要根据具体的游戏需求进行调整和改进。
下一篇:保持向量的值