使人物移动和跳跃:
Rigidbody2D rb; public float speed = 8f; //移动速度 public float jumpForce = 6.3f; //跳跃速度 bool jumpPressed; //是否按下跳跃键 void Start() { rb = Component<Rigidbody2D>(); //获取刚体组件 } void Update() { if(input.GetButtonDown("Jump") jumpPressed = true; void FixedUpdate() { MoveMent(); //调用函数 } void MoveMent() { xVelocity = input.GetAxis("Horizontal"); //检测是否按下AD建或←→建,此方法返回-1——1之间的数。 if(xVelocity) { rb.velocity = new Vector2(speed * xVelocity * Time.fixedDeltaTime * 60, rb.velocity.y); //移动 } if(jumpPressed) { rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse); //给刚体突然添加一个力,来实现跳跃效果 jumpPressed = false; } }