[Chip]Unity开发笔记--Input System实现跳跃,冲刺

本文详细介绍了如何在Unity中使用InputSystem配置移动和冲刺功能,包括Jump和Dash动作的设置,以及相应的C#脚本编写,涉及碰撞检测和状态管理。
摘要由CSDN通过智能技术生成

0.基础配置

 基础配置在上一期中有介绍

[Chip]Unity开发笔记--Input System实现移动

1.Input Actions配置

1.在Actions中右键Add Action命名为Jump,Action Type为Button。

2.右键Jump选择Add Binding,在右侧Path中选择Listen按下空格键,即可将空格键绑定为跳跃所需的按键。

3.同理Add Action命名为Dash,将Shift键绑定为冲刺所需的按键。

配置完后如图所示

4.保存后关闭窗口。

2.跳跃代码编写

1.右键Create一个C# Scrpt命名为Jump,并挂载到Player身上。

2.初始化PlayerControl

public class Jump : MonoBehaviour
{
    public PlayerControl playerControl;
    private void Awake()
    {
        playerControl = new PlayerControl();
    }
    private void OnEnable()
    {
        playerControl.Enable();
    }
    private void OnDisable()
    {
        playerControl.Disable();
    }
}

3.为Player施加力,并限定跳跃次数

public float jumpForse;
public bool canJump;
private void Start()
    {
        rb=GetComponent<Rigidbody>();
        canJump=true;
        playerControl.Player.Jump.started += JumpStart;
        //为跳跃注册事件
    }
private void JumpStart(InputAction.CallbackContext obj)
    {
        if(canJump)
        {
            canJump=false;
            rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
            //初始化垂直速度
            rb.AddForce(transform.up * jumpForse, ForceMode.Impulse);
            //在垂直方向上为Player施加一个瞬时的力(ForceMode.Impulse)
        }
    }

4.检测是否接触地面

public LayerMask ground;
public float groundCheckSize;
public bool inGround;
private void Update()
    {
        GroundCheck();
    }
private void GroundCheck()
    {
        inGround = Physics.Raycast(transform.position, Vector3.down, groundCheckSize, ground);
        //向下发射射线,检测是否碰到了层级为ground的物体,碰到返回true,所需参数分别为:射线起点,射线方向,射线长度,层级
    }

5.接触地面后重置跳跃次数

private void ResetJump()
    {
        if(inGround)
        {
            canJump=true;
        }
    }

设置好jumpForse和ground保存后运行即可使用空格键跳跃。

3.冲刺代码编写

1.右键Create一个C# Scrpt命名为Dash,并挂载到Player身上。

2.以下代码用于初始化PlayerControl

public class Dash : MonoBehaviour
{
    private void Awake()
    {
        playerControl = new PlayerControl();
    }
    private void OnEnable()
    {
        playerControl.Enable();
    }
    private void OnDisable()
    {
        playerControl.Disable();
    }
}

3.获取输入方向并设置为冲刺方向

Rigidbody rb;
Vector2 inputVector;
Vector3 dashVector;
bool canDash;
bool isDash;
private void Start()
    {
        rb = GetComponent<Rigidbody>();
        canDash=true;
        playerControl.Player.Dash.performed += TapDash;
        //为冲刺注册事件
    }
private void Update()
    {
        inputVector = playerControl.Player.Move.ReadValue<Vector2>();
        //获取输入方向
    }
private void TapDash(InputAction.CallbackContext obj)
    {
        if (inputVector.x != 0 || inputVector.y != 0)
        //确保冲刺时至少有一个方向上的值不为0
        {
            if (canDash&&!inGround)
            //确保冲刺时Player在空中
            { 
                canDash = false;
                isDash=true;
                dashVector = transform.right * inputVector.x + transform.forward * inputVector.y;
                //将输入方向设置为冲刺方向
            }
        }
    }

4.开始冲刺

public float dashForce;
public float dashTime;
public float dashCountTimer;
private void FixedUpdate()
    {
        if (isDash)
        {
            TapDashing();
        }
    }
private void TapDashing()
    {
        rb.useGravity = false;
        rb.velocity=new Vector3(rb.velocity.x,0,rb.velocity.z);
        //设置Player不受重力影响,并设置垂直方向速度为0使Player的高度保持不变
        rb.AddForce(dashVector.normalized * dashForce, ForceMode.Force); 
        dashCountTimer = dashTime;
        dashCountTimer -= Time.deltaTime;
        //持续为Player施加力直到冲刺时间到达所设定的时间
        if (dashCountTimer <= 0)
        {
            StopTapDash();
        }
    }

private void StopTapDash()
    {
        isDash=false;
        rb.useGravity = true;
    }

5.接触地面后重置冲刺次数

private void ResetDash()
    {
        if(inGround)
        {
            canDash=true;
        }
    }

6.设置好dashForce和dashTime保存并运行即可在空中使用Shift冲刺

4.状态相关

此时在运行时会发现,多个Action可同时进行产生冲突,此时需要使用有限状态机相关的知识对Player的Action进行控制,设定优先级保证同一时间只处于一个状态,例如:冲刺时禁用键盘输入,使Player在冲刺过程中不能进行移动,此处将不做过多介绍。

5.完整项目视频预览

[Unity练习作]类幽灵行者跑酷小游戏

  • 13
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值