3月19日
今天整一个系列,完成移动的总结
包括:
左右移动,跳跃,二段跳
进一步尝试:
滑墙,冲刺,
首先是常规的移动和跳跃
void Update()
{
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
Vector2 dir = new Vector2(x, y);
move(dir);
if (Input.GetKeyDown(KeyCode.Space))
{
jump();
}
}
private void move(Vector2 dir)
{
rb.velocity = (new Vector2(dir.x * speed, rb.velocity.y));
}
private void jump()
{
rb.velocity = new Vector2(rb.velocity.x, 0);
rb.velocity += Vector2.up * jumpForce;
}
然后,想要实现根据按键时间长短,来控制跳跃高度,查看了一个github案例,采用添加了一个跳跃优化脚本
void Update()
{
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * Physics2D.gravit