Unity-SunnyLand练习记录4-角色的跳跃及手感优化
Unity-SunnyLand练习记录4-角色的跳跃及手感优化
角色跳跃代码
-
在FixedUpdate中,判断是否按下跳跃键,并调用跳跃函数
1
2
3
4
5
6
7
8
9
10private void FixedUpdate()
{
PlayerMobement();
//判断并调用
if(Input.GetButton("Jump"))
{
jump();
}
} -
在跳跃函数中,为角色刚体添加y轴方向的速度
1
2
3
4void jump()//角色跳跃函数
{
playerRb.velocity = new Vector2(playerRb.velocity.x, jumpForce*Time.fixedDeltaTime);
}
手感优化
-
在FixedUpdate中,我们使用GetButton,在Update中,我们使用GetButtonDown
-
用户手册对两者的描述:
1
GetButton:bool 当按下并且未释放轴时,返回 true。松开按钮时,GetButton 将返回 /false/。
1
2
3GetButtonDown:
在用户按下由 buttonName 标识的虚拟按钮的帧期间返回 true。
Call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again.
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 星辰微光的博客!





