- Home /
How to code a burst of speed?
Hello everyone! I have to code a little burst of speed for my 2D shuttle, and I need some help! So, I got this code
public class ShuttleMovement : MonoBehaviour
{ float speedForce = 10f; float torqueForce = 3f; public ShuttleMovement() { }
void Start()
{
}
void Update() { }
void FixedUpdate()
{
Rigidbody2D rb = GetComponent<Rigidbody2D>();
if (Input.GetButton("Accelerate"))
{
rb.AddForce( transform.up * speedForce);
}
rb.AddTorque(Input.GetAxis("Horizontal") * torqueForce);
}
}
the speedforce is just the normal speed whenever I press W, and the torque is for turning left or right, anyways, what I want is that whenever I press the spacebar, the forwards speed of the shuttle goes up for about 2 seconds, let's say, the normal speed(speedForce) is 10f, but whenever I press the spacebar, it get's up to 15f for about 2 seconds, then it slows down to normal Thanks!
Answer by ashcrafttheilluminati · Jun 15, 2020 at 06:00 AM
add a new variable for a speed boost, apply it to your player and bind that function to the button.
for example:
if (Input.GetKeyDown(KeyCode.Space)) { playerRb.AddForce(focalPoint.transform.forward * speedBoost, ForceMode.Impulse); }
I couldn't get it to work, if I add a new variable with a higher speed, is there a way I could just set a timer with GetKeyDown?
Your answer
Follow this Question
Related Questions
How can I enable my capsule to move; object 'target' not recognized. 0 Answers
HOW TO MAKE A NPC GO FORWAR 0 Answers
How can i make it so that the object doesnt instantly go to top speed. Here is my code 1 Answer
MoveTowards is moveing my object to random position when i click it is already on a way 1 Answer