using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
/// <summary>
/// 用户按下WASD键可以控制目标前后左右移动
/// 用户滑动鼠标可以控制目标上下左右旋转
/// </summary>
public float MoveSpeed = 10f;// 定义一个可以在外部更改的移动速度参数
public float RotateSpeed = 100f;// 定义一个可以在外部更改的旋转速度参数
float RotationY;
float RotationX;
// Update is called once per frame
void Update()
{
float MoveLR = Input.GetAxis("Horizontal");//用户按下AD键返回一个(-1,1)的值控制目标左右移动
float MoveFB = Input.GetAxis("Vertical");//用户按下WS键返回一个(-1,1)的值控制目标前后移动
this.transform.Translate(new Vector3(MoveLR * 1, 0, MoveFB * 1) * MoveSpeed * Time.deltaTime, Space.Self);
float RotateY = Input.GetAxis("Mouse X");
float RotateX = Input.GetAxis("Mouse Y");
RotationY += RotateY * RotateSpeed * Time.deltaTime;//用来累积存储用户每次水平移动鼠标得出的值乘以速度
RotationX += RotateX * RotateSpeed * Time.deltaTime;//用来累积存储用户每次垂直移动鼠标得出的值乘以速度
this.transform.rotation = Quaternion.Euler(RotationX, RotationY, 0);
LimitRotate();
}
//用户垂直移动鼠标时,限制角色上下旋转的最大角度和单次旋转角度
void LimitRotate()
{
RotationX = Mathf.Clamp(RotationX, -30f, 30f);
}
//限制鼠标上下旋转角度2
//void LimitRotate()
//{
// if (RotationX > 360)
// {
// RotationX -= 360;
// }
// if (RotationX < 360)
// {
// RotationX += 360;
// }
//}
}//end class
多行代码转注释快捷键:[CTRL+K]+[CTRL+C]
多行注释转代码快捷键:[CTRL+K]+[CTRL+U]