我有一个问题,我不能跳,但当我删除了代码.的时候,我就不能跳了。
rb.velocity = Vector2.right * vel;
它的工作,那么为什么我不能在同一时间使用它们,我如何解决它?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
public Rigidbody2D rb;
public float vel = 7.5f;
public float jump_vel = 5f;
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
if (Input.GetKeyDown("space"))
{
rb.velocity += Vector2.up * jump_vel;
}
rb.velocity = Vector2.right * vel;
}
}
它不会工作,因为你覆盖了 rb.velocity
的计算。if
第二次转让的声明 rb.velocity
; (rb.velocity = Vector2.right * vel;
).
(编辑)要解决这个问题,只要用。
Vector3 jumpVelocity = Vector3.zero;
if (Input.GetKeyDown("space"))
{
jumpVelocity = Vector2.up * jump_vel;
}
rb.velocity = Vector2.right * vel + jumpVelocity;