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在冲刺过程中不能进行移动,此处将不做过多介绍。