人物移动脚本
public class Motion : MonoBehaviour
{
#region Fields
public float speed;
private Rigidbody rig;
#endregion
#region MonoBehaviour CallBacks
private void Start()
{
Camera.main.enabled = false;
rig = GetComponent<Rigidbody>();
}
// 多人游戏下同步玩家位置时建议使用FixedUpdate
void FixedUpdate()
{
// 玩家移动
float t_hmove = Input.GetAxis("Horizontal");
float t_vmove = Input.GetAxis("Vertical");
Vector3 t_direction = new Vector3(t_hmove, 0, t_vmove);
t_direction.Normalize();
rig.velocity = transform.TransformDirection(t_direction) * speed * Time.deltaTime;
}
#endregion
#region Methods
#endregion
}
查看四周脚本
public class Look : MonoBehaviour
{
#region Fields
public Transform player;
public Transform cams;
public float xSensitivity;
public float ySensitivity;
public float maxAngle;
private Quaternion camCenter;
#endregion
#region MonoBehaviour CallBacks
void Start()
{
camCenter = cams.localRotation;
}
void Update()
{
SetY();
SetX();
}
#endregion
#region Methods
// 上下查看
void SetY()
{
float t_input = Input.GetAxis("Mouse Y") * ySensitivity * Time.deltaTime;
Quaternion t_adj = Quaternion.AngleAxis(t_input, -Vector3.right);
Quaternion t_detal = cams.localRotation * t_adj;
// 限制上下查看的最大角度
if (Quaternion.Angle(camCenter, t_detal) < maxAngle)
{
cams.localRotation = t_detal;
}
}
// 左右查看
void SetX()
{
float t_input = Input.GetAxis("Mouse X") * xSensitivity * Time.deltaTime;
Quaternion t_adj = Quaternion.AngleAxis(t_input, Vector3.up);
Quaternion t_detal = player.localRotation * t_adj;
player.localRotation = t_detal;
}
#endregion
}
靓照