堆栈溢出的人。我想为我的个人学校项目制作无人机游戏。一切都很顺利,但我现在面临一个问题。当我按下所选按钮时,我的无人机脚本不会使无人机保持恒定高度。换句话说,当我按下 E 或 Q 时,无人机会持续上升或持续下降。只要我按下选定的键,我就希望它上升。我该怎么做?
代码->
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Levitating : MonoBehaviour
{
Rigidbody ourDrone;
private void Awake()
{
ourDrone = GetComponent<Rigidbody>();
}
// Make the drone default velocity the same value of the gravity acceleration, with opposite direction
private Vector3 DRONE_DEFAULT_VELOCITY = new Vector3(0, 9.81f, 0);
public float upForce;
private void Update()
{
MovementUpDown();
}
private void FixedUpdate()
{
ourDrone.AddRelativeForce(DRONE_DEFAULT_VELOCITY * upForce);
}
void MovementUpDown()
{
if (Input.GetKey(KeyCode.E))
{
upForce = 15;
}
else if (Input.GetKey(KeyCode.Q))
{
upForce = -3;
}
else
{
// Resetting the force muultiplier
upForce = 1;
}
}
}