跳跃时我的球员运动有问题

时间:2020-05-15 23:31:22

标签: c# visual-studio unity3d

我面对的唯一问题是,玩家只有在他们建立动量时才会跳动,而我不希望我想让玩家在站立或移动时按空格键跳动,而我尝试了speed.y = jumphight,这也不起作用

const humanProps = ["weight", "height", "age", "shoeSize"] as const; // added prop

const alice: Human = { // error! 
   age: 31,
   height: 176,
   weight: 47
};

const humanPropLabels: Readonly<Record<HumanProp, string>> = { // error!
   weight: "Weight (kg)",
   height: "Height (cm)",
   age: "Age (full years)"
};

function describe(human: Human): string { // okay
   let lines: string[] = [];
   for (const key of humanProps) {
      lines.push(`${humanPropLabels[key]}: ${human[key]}`);
   }
   return lines.join("\n");
}

2 个答案:

答案 0 :(得分:1)

您可以使用ForceMode力在刚体上使用addforce,如果您不想根据fps使用物理,请使用FixedUpdate而不是update

   GetComponent<Rigidbody>().AddForce(Vector3.up,ForceMode.Force);

答案 1 :(得分:0)

Vector3 velocity;
public float gravity = -9.82f;
public float JumpHeight = 3f;

if(Input.GetButtonDown("Jump")
{
  velocity.y = Matf.Sqrt(JumpHeight * -2f * gravity);
}

您也可以按照他人的说法去做,这是因为该人的代码行太弱了,所以可能行不通。这是所述代码的更新版本。

GetComponent<Rigidbody>().AddForce(Vector3.up * 10f ,ForceMode.Impulse);

我使用*的原因是为了增加“ up”变量,因为它意味着Vector3(0f,1f,0f);并使用ForceMode.Impulse使其更易变且更坚固。

希望此代码有助于您制作游戏, 亲切的问候。 SB