76 lines
1.8 KiB
C#
76 lines
1.8 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class RoleMove : MonoBehaviour
|
||
{
|
||
public float horizontalinput;//水平参数
|
||
public float Verticalinput;//垂直参数
|
||
float speed = 10.0f;//声明一个参数,没有规定
|
||
public float sensitivityHor = 3f;
|
||
|
||
// 垂直视角移动的敏感度
|
||
|
||
public float sensitivityVer = 3f;
|
||
|
||
// 视角向上移动的角度范围,该值越小范围越大
|
||
|
||
public float upVer = -40;
|
||
|
||
// 视角向下移动的角度范围,该值越大范围越大
|
||
|
||
public float downVer = 45;
|
||
|
||
// 垂直旋转角度
|
||
|
||
private float rotVer;
|
||
|
||
void Start()
|
||
{
|
||
rotVer = transform.eulerAngles.x;
|
||
}
|
||
//在update中书写
|
||
void Update()
|
||
{
|
||
|
||
horizontalinput = Input.GetAxis("Horizontal");
|
||
//AD方向控制
|
||
Verticalinput = Input.GetAxis("Vertical");
|
||
|
||
if (horizontalinput != 0 && Verticalinput != 0)
|
||
{
|
||
horizontalinput = horizontalinput * 0.6f;
|
||
Verticalinput = Verticalinput * 0.6f;
|
||
}
|
||
//WS方向控制
|
||
this.transform.Translate(Vector3.right * horizontalinput * Time.deltaTime * speed);
|
||
//控制该物体向侧方移动
|
||
this.transform.Translate(Vector3.forward * Verticalinput * Time.deltaTime * speed);
|
||
|
||
// 获取鼠标上下的移动位置
|
||
|
||
float mouseVer = Input.GetAxis("Mouse Y");
|
||
|
||
// 获取鼠标左右的移动位置
|
||
|
||
float mouseHor = Input.GetAxis("Mouse X");
|
||
|
||
// 鼠标往上移动,视角其实是往下移,所以要想达到视角也往上移的话,就要减去它
|
||
|
||
rotVer -= mouseVer * sensitivityVer;
|
||
|
||
// 限定上下移动的视角范围,即垂直方向不能360度旋转
|
||
|
||
rotVer = Mathf.Clamp(rotVer, upVer, downVer);
|
||
|
||
// 水平移动
|
||
|
||
float rotHor = transform.localEulerAngles.y + mouseHor * sensitivityHor;
|
||
|
||
// 设置视角的移动值
|
||
|
||
transform.localEulerAngles = new Vector3(rotVer, rotHor, 0);
|
||
|
||
}
|
||
}
|