【unity】角色移动控制,移动,跳跃,地面监测

12 篇文章 0 订阅

场景

  • 做一个简单的任务移动控制
  • wasd的键盘移动
  • 跳跃
  • 地面检测
  • 跳跃时保留地速

1.character controller

  • 调整好角色控制器,通过角色控制器的API来进行操作角色移动
    在这里插入图片描述

2.通过键盘操控前后左右移动

  • 因为速度是通过键盘每帧的输入进行累加的,因此先创建一个速度向量
Vector3 motionVector = Vector3.zero;
  • 获取键盘输入的浮点数
float horizontal_axis = Input.GetAxis("Horizontal");
float vertical_axis = Input.GetAxis("Vertical");
  • 对速度变量进行叠加
  • 注意这里是对角色自身的方向来移动
  • 注意下面这个wasd方向的位移结构都是:1.方向单位向量 2.速度 3.输入 4.(非必须)时间间隔,换成0.02也是可以的
  • 注意一定是速度的累加,因为键盘输入的值是会变的,是慢慢增长的,松开是会回落的
motionVector += playerTransform.forward * (moveSpeed * vertical_axis * Time.fixedDeltaTime);
motionVector += playerTransform.right * (moveSpeed * horizontal_axis * Time.fixedDeltaTime);
  • 这样简单的前后左右移动就可以了
characterController.Move(motionVector);

3.重力作用

  • 根据物理公式,自由落体的速度公式是:Vt = gt,也就是加速度 * 时间,下面即使速度的大小
verticalVelocity -= gravity * Time.fixedDeltaTime;
  • 然后将速度叠加到全局坐标系的垂直方向,这样就可以实现下落
motionVector += Vector3.up * (verticalVelocity * Time.fixedDeltaTime);

4.地面检测

  • 因为如果不用地面检测将垂直方向的速度置为0,那么角色即使落地停止,但是它的垂直方向的速度还是会一直随着时间叠加。
  • 因此需要进行地面检测,接触地面后,垂直方向的速度置为0
  • 地面检测可以通过很多种方式(可以通过ray射线,也可以通过碰撞)
  • 这里使用的方式是后者,注意在Player的character controller下方圆截面中心点,放置一个地面监测点
    在这里插入图片描述
  • 这样通过下面这个方法,会以这个中心点,以一个半径的距离来检测地面(isGround 是一个bool值)
isGround = Physics.CheckSphere(GroundCheckPoint.position, checkShereRadius, GroundLayer);
  • 当然这里也是有点坑的(1.需要给需要检测的物体上一个LayerMask;2.需要考虑一下character的Skin Width皮肤厚度,因此半径应该等于character的Radius 加上 Skin Width

5.跳跃

  • 将世界坐标下的垂直方向的速度改成向上的一个初速度就可以了
  • 那么这个速度是多少呢?需要通过最大跳跃高度来计算比较合适。
public float MaxJumpHeight = 5f;
  • 因为垂直上抛运动的上升过程和下降过程是镜像对称的(高中知识),因此起跳速度应该是自由落体MaxJumpHeight 高度的下落速度的反向速度。
  • 公式是 Vt = 根号下(2gh)
  • 因此这样计算比较合理
if (isGround)
{
    if (Input.GetButtonDown("Jump"))
    {
        //因为有最高的跳跃高度规定,用高中的公式来计算,上跳和下落就是对称的运动vt = gt; 2h/g开平方 = t,
        //那么h高度时刻速度应该是vt = g*根号下(2h/g) = 根号下2gh
        verticalVelocity = Mathf.Sqrt(2 * gravity * MaxJumpHeight);
        //跳跃的时候应该记录一下起跳瞬间的水平方向的速度,保持不变
    }
}

6.跳跃的时候保持地速

  • 这样比较真实。。不然跳上箱子什么的,通过助跑也上不去。。回原地跳
  • 声明两个用于记录起跳时瞬间的水平速度
private float tmp_horizontal_axis;
private float tmp_vertical_axis;
  • 然后在起跳的瞬间,记录一下当时的键盘输入的float浮点值
if (isGround)
{
    if (Input.GetButtonDown("Jump"))
    {
        tmp_horizontal_axis = horizontal_axis;
        tmp_vertical_axis = vertical_axis;
        //因为有最高的跳跃高度规定,用高中的公式来计算,上跳和下落就是对称的运动vt = gt; 2h/g开平方 = t,
        //那么h高度时刻速度应该是vt = g*根号下(2h/g) = 根号下2gh
        verticalVelocity = Mathf.Sqrt(2 * gravity * MaxJumpHeight);
        //跳跃的时候应该记录一下起跳瞬间的水平方向的速度,保持不变
    }
}
  • 然后如果置空的时候,就用这个记录的空速来运动
if (isGround)
{
    motionVector += playerTransform.forward * (moveSpeed * vertical_axis * Time.fixedDeltaTime);
    motionVector += playerTransform.right * (moveSpeed * horizontal_axis * Time.fixedDeltaTime);
}
else
{
    //跳起来之后是有水平方向速度的
    motionVector += playerTransform.forward * (moveSpeed * tmp_vertical_axis * Time.fixedDeltaTime);
    motionVector += playerTransform.right * (moveSpeed * tmp_horizontal_axis * Time.fixedDeltaTime);
}
  • 再优化一下~在地面上的时候才接收键盘的输入,这样就比较完美了!
float horizontal_axis = 0f;
float vertical_axis = 0f;

if (isGround)
{
    horizontal_axis = Input.GetAxis("Horizontal");
    vertical_axis = Input.GetAxis("Vertical");
    motionVector += playerTransform.forward * (moveSpeed * vertical_axis * Time.fixedDeltaTime);
    motionVector += playerTransform.right * (moveSpeed * horizontal_axis * Time.fixedDeltaTime);
}
else
{
    //跳起来之后是有水平方向速度的
    motionVector += playerTransform.forward * (moveSpeed * tmp_vertical_axis * Time.fixedDeltaTime);
    motionVector += playerTransform.right * (moveSpeed * tmp_horizontal_axis * Time.fixedDeltaTime);
}

7.完整的代码如下

  • 记录而已,有不好的地方请指正
  • 下面是包含了键盘移动和鼠标控制视野的代码
/// <summary>
/// 角色移动和视角移动
/// </summary>
public class PlayerMovementController : MonoBehaviour
{
    [Space(20)] public float rotateSpeed = 180;
    [Range(1, 2)] public float rotateRatio = 1;
    public Transform playerTransform;
    public Transform eyeViewTransform;
    public float MaxViewAngle = 65f;
    private float tmp_viweRotationOffset;
    public float gravity = 9.8f;
    public float verticalVelocity = 0;
    public bool isGround = false;
    public LayerMask GroundLayer;
    public Transform GroundCheckPoint;
    public float checkShereRadius;
    public float MaxJumpHeight = 5f;
    private float tmp_horizontal_axis;
    private float tmp_vertical_axis;

    //move
    [Space(20)] public CharacterController characterController;
    public float moveSpeed = 10;

    private void Start()
    {
        characterController = this.GetComponent<CharacterController>();
    }

    private void FixedUpdate()
    {
        PlayerRotateControl();
        PlayerMoveControl();
    }

    /**
     * 通过键盘控制角色移动
     */
    public void PlayerMoveControl()
    {
        if (characterController == null)
        {
            return;
        }

        Vector3 motionVector = Vector3.zero;
        //获取键盘输入
        float horizontal_axis = 0f;
        float vertical_axis = 0f;
        //注意这里是对角色自身的方向来移动
        //注意下面这个wasd方向的位移结构都是:1.方向单位向量 2.速度 3.输入 4.(非必须)时间间隔,换成0.02也是可以的
        //注意一定是速度的累加,因为键盘输入的值是会变的,是慢慢增长的,松开是会回落的
        if (isGround)
        {
            horizontal_axis = Input.GetAxis("Horizontal");
            vertical_axis = Input.GetAxis("Vertical");
            motionVector += playerTransform.forward * (moveSpeed * vertical_axis * Time.fixedDeltaTime);
            motionVector += playerTransform.right * (moveSpeed * horizontal_axis * Time.fixedDeltaTime);
        }
        else
        {
            //跳起来之后是有水平方向速度的
            motionVector += playerTransform.forward * (moveSpeed * tmp_vertical_axis * Time.fixedDeltaTime);
            motionVector += playerTransform.right * (moveSpeed * tmp_horizontal_axis * Time.fixedDeltaTime);
        }
        //跳跃和下落

        //垂直方向应该取得是视界坐标的朝向
        //这里需要自由落体的公式 h = 0.5 * g * t * t 因此 下落的所读就是 Vt = g * t
        //先实现重力 , 垂直方向的速度是累加的!
        verticalVelocity -= gravity * Time.fixedDeltaTime;
        motionVector += Vector3.up * (verticalVelocity * Time.fixedDeltaTime);
        //在角色接触到地面的时候,向下的速度归零
        //地面的碰撞检测可以使用,碰撞,也可以使用射线,这里使用一个checkPoint,放在胶囊体下方的原型截面线上
        //CheckSphere检车某个点为圆心的球体内是否有碰撞体,加入 verticalVelocity < 0 判断是防止跳跃的时候碰到物体,但是跳跃的动作还没结束,V就强制为0了
        isGround = Physics.CheckSphere(GroundCheckPoint.position, checkShereRadius, GroundLayer);
        if (isGround && verticalVelocity < 0)
        {
            isGround = true;
            verticalVelocity = 0;
        }

        //写跳跃的脚本
        if (isGround)
        {
            if (Input.GetButtonDown("Jump"))
            {
                tmp_horizontal_axis = horizontal_axis;
                tmp_vertical_axis = vertical_axis;
                //因为有最高的跳跃高度规定,用高中的公式来计算,上跳和下落就是对称的运动vt = gt; 2h/g开平方 = t,
                //那么h高度时刻速度应该是vt = g*根号下(2h/g) = 根号下2gh
                verticalVelocity = Mathf.Sqrt(2 * gravity * MaxJumpHeight);
                //跳跃的时候应该记录一下起跳瞬间的水平方向的速度,保持不变
            }
        }

        characterController.Move(motionVector);
    }

    /**
     * 鼠标控制角色视角移动
     */
    private void PlayerRotateControl()
    {
        if (playerTransform == null || eyeViewTransform == null)
        {
            return;
        }

        //x控制的是水平方向旋转,也就是控制沿Y轴旋转,控制Player的水平方向转动
        float offset_x = Input.GetAxis("Mouse X");
        //控制垂直方向旋转,也就是沿x轴旋转,这里只控制EYE_VIEW照相机在垂直方向的转动
        float offset_y = Input.GetAxis("Mouse Y");
        playerTransform.Rotate(Vector3.up * (offset_x * rotateSpeed * rotateRatio * Time.fixedDeltaTime));
        //对旋转角进行累加才能够进行对最大角度的限制
        tmp_viweRotationOffset -= offset_y * rotateSpeed * rotateRatio * Time.fixedDeltaTime;
        //限制一下旋转的角度
        tmp_viweRotationOffset = Mathf.Clamp(tmp_viweRotationOffset, -MaxViewAngle, MaxViewAngle);
        Quaternion EyeLocalQuaternion = Quaternion.Euler(new Vector3(tmp_viweRotationOffset,
            eyeViewTransform.localEulerAngles.y,
            eyeViewTransform.localEulerAngles.z));
        //注意旋转角是四元素,需要用四元素处理
        eyeViewTransform.localRotation = EyeLocalQuaternion;
    }
}
  • 9
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
A: 在Unity中,角色移动可以通过以下方法进行控制: 1. 使用Unity提供的标准角色控制器组件。 2. 编写自己的移动控制脚本。 下面分别介绍这两个方法: 1. 使用Unity提供的标准角色控制器组件。 Unity提供了标准角色控制器组件,可以方便地实现基本的角色移动控制。 步骤如下: 1. 创建一个游戏对象,给它添加一个标准角色控制器组件。 2. 在脚本中定义一个向量变量,表示角色前后左右移动的方向。 3. 在Update函数中,根据键盘输入更新角色移动方向。 4. 调用角色控制器组件的Move函数进行移动。 示例代码如下: ``` using UnityEngine; public class PlayerController : MonoBehaviour { public float moveSpeed = 5.0f; // 移动速度 private Vector3 targetDirection; // 目标移动方向 private CharacterController characterController; void Start() { characterController = GetComponent<CharacterController>(); } void Update() { // 获取键盘输入 float h = Input.GetAxisRaw("Horizontal"); float v = Input.GetAxisRaw("Vertical"); // 计算目标移动方向 targetDirection = new Vector3(h, 0, v).normalized; // 移动角色 characterController.Move(targetDirection * moveSpeed * Time.deltaTime); } } ``` 2. 编写自己的移动控制脚本。 如果标准角色控制器组件不能满足需求,可以编写自己的角色移动控制脚本。 步骤如下: 1. 在脚本中定义一个向量变量,表示角色前后左右移动的方向。 2. 在Update函数中,根据键盘输入更新角色移动方向。 3. 调用角色的Transform.Translate函数进行移动。 示例代码如下: ``` using UnityEngine; public class PlayerController : MonoBehaviour { public float moveSpeed = 5.0f; // 移动速度 private Vector3 targetDirection; // 目标移动方向 void Update() { // 获取键盘输入 float h = Input.GetAxisRaw("Horizontal"); float v = Input.GetAxisRaw("Vertical"); // 计算目标移动方向 targetDirection = new Vector3(h, 0, v).normalized; // 移动角色 transform.Translate(targetDirection * moveSpeed * Time.deltaTime, Space.World); } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值