角色控制器移动玩家上斜坡卡在坡上的问题怎么解决,代码如下
CharacterController playerController;
Vector3 direction;
public float speed = 5;
public float Runspeed = 10;
public float jumpPower = 5;
public float gravity = 7f;
public float mousespeed = 5f;
public float minmouseY = -45f;
public float maxmouseY = 45f;
float RotationY = 0f;
float RotationX = 0f;
public Transform agretctCamera;
// Use this for initialization
void Start()
{
playerController = this.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
//获取世界按键方向
float _horizontal = Input.GetAxis("Horizontal");
float _vertical = Input.GetAxis("Vertical");
if (playerController.isGrounded)//跳跃
{
direction = new Vector3(_horizontal, 0, _vertical);
if (Input.GetKeyDown(KeyCode.Space))
direction.y = jumpPower;
}
direction.y -= gravity * Time.deltaTime;
playerController.Move(playerController.transform.TransformDirection(direction * Time.deltaTime * speed));//移动
RotationX += agretctCamera.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mousespeed;//视觉转动
RotationY -= Input.GetAxis("Mouse Y") * mousespeed;
RotationY = Mathf.Clamp(RotationY, minmouseY, maxmouseY);
this.transform.eulerAngles = new Vector3(0, RotationX, 0);
agretctCamera.transform.eulerAngles = new Vector3(RotationY, RotationX, 0);
//疾跑
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
{
playerController.Move(playerController.transform.TransformDirection(direction * Time.deltaTime * Runspeed));
jumpPower = 2f;
Camera.main.fieldOfView = 70;
GameObject.Find("Canvas/状态/奔跑特效").SetActive(true);
GameObject.Find("Me/Main Camera/粒子特效").SetActive(true);
}
else
{
jumpPower = 3f;
Camera.main.fieldOfView = 60;
GameObject.Find("Canvas/状态/奔跑特效").SetActive(false);
GameObject.Find("Me/Main Camera/粒子特效").SetActive(false);
//摇杆控制移动
Vector3 dir = new Vector3(JoyStick.h, 0, JoyStick.v);//获取摇杆脚本方向
if (dir.sqrMagnitude > 0)
{
playerController.Move(playerController.transform.TransformDirection(dir * Time.deltaTime * speed));
}
}