更好的实现方法
using UnityEngine;
public class CameraMove : MonoBehaviour
{
private const float MoveSpeed = 0.15f;
private static readonly Vector3 MinPos = new Vector3(-14, 14, -12);
private static readonly Vector3 MaxPos = new Vector3(11, 14, 15);
private static readonly Vector3 InitPos = new Vector3(0, 14, 0);
private void Start()
{
transform.position = InitPos;
}
private void Update()
{
var trans = transform;
var position = trans.position;
var x = Input.GetAxis("Horizontal");
var z = Input.GetAxis("Vertical");
var newX = position.x + x * MoveSpeed;
var newZ = position.z + z * MoveSpeed;
newX = Mathf.Clamp(newX, MinPos.x, MaxPos.x);
newZ = Mathf.Clamp(newZ, MinPos.z, MaxPos.z);
trans.position = new Vector3(newX, position.y, newZ);
}
}