using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeiMove : MonoBehaviour
{
private CharacterController controller;
public float Speed = 10F; //移动速度
public float RotateSpeed = 1f; //旋转速度
public float Gravity = -5f; //自建重力加速度
private Vector3 v3 = Vector3.zero;
public Transform GroundCheck; //地面检测
public float CheckRadius = 0.2f; //物理的检测半径
private bool IsGround;
public LayerMask layerMask; //层标记
public float JumpHeight = 3f; //跳跃高度
void Start()
{
controller = transform.GetComponent<CharacterController>();
}
void FixedUpdate ()
{
MoveLikeWow();
}
private void MoveLikeWow()
{
IsGround = Physics.CheckSphere(GroundCheck.position,CheckRadius, layerMask);//检测与地面的碰撞
if (IsGround && v3.y<0)
{
v3.y = 0;
}
if (IsGround && Input.GetButtonDown("Jump") ) //只有在地面上才能跳,不允许空中连跳
{
v3.y += Mathf.Sqrt(JumpHeight * -2 * Gravity);
}
var x = Input.GetAxis("Horizontal"); //水平ad控制方向旋转
var z = Input.GetAxis("Vertical"); //垂直ws控制前进后退
var move = transform.forward * Speed * z * Time.deltaTime;
controller.Move(move);
transform.Rotate(Vector3.up,x*RotateSpeed); //绕Y轴旋转
v3.y += Gravity * Time.deltaTime; //重力加速度*持续时间=速度
controller.Move(v3 * Time.deltaTime); //速度*时间=距离(x,z一直为0)
}
}
大伟 类似魔兽世界角色控制
最新推荐文章于 2021-10-20 15:19:24 发布