- Home /
How do I add continuous force on an object?
Hello, I'm a beginner in coding and programming, and I've been having complications on my object movement.
Currently, I have my script set up with a forward force, and a sideways force. My forward force is continuous as I didn't bind a key input for it.
The script I have for the sideways force is:
if ( Input.GetKey("d")) { // Only executed if condition is met rb.AddForce(sidewaysForce Time.deltaTime, 0, 0, ForceMode.VelocityChange); }
if (Input.GetKey("a")) { rb.AddForce(-sidewaysForce Time.deltaTime, 0, 0, ForceMode.VelocityChange); }
Every time I press one of the keys, it adds a force on the object on the x axis, causing my object to move sideways. I can hold the key to have it keep on moving, but as soon as I let go, it stops. What I'm trying to do is make it have continuous force upon a single press of one of the keys.
Basically, I'm trying to have it go left automatically or continuously upon a single press of the "a" key and have it change direction with the "d" key while still having continuous force.
I've been searching for solutions to this problem, but I've had no luck there.
Does anyone have an idea or advice on how I may be able to solve this issue?
Answer by Llama_w_2Ls · Jul 26, 2020 at 08:22 AM
bool ContinuousMovementToLeft = false;
bool ContinuousMovementToRight = false;
Rigidbody rb;
float MovementSpeed = 400;
private void Update()
{
if (ContinuousMovementToLeft == true)
{
rb.AddForce(-Vector3.right * MovementSpeed);
}
else if (ContinuousMovementToRight == true)
{
rb.AddForce(Vector3.right * MovementSpeed);
}
CheckIfKeysWerePressed();
}
void CheckIfKeysWerePressed()
{
if (Input.GetKeyDown(KeyCode.A))
{
ContinuousMovementToLeft = true;
ContinuousMovementToRight = false;
}
else if (Input.GetKeyDown(KeyCode.A))
{
ContinuousMovementToLeft = false;
ContinuousMovementToRight = true;
}
}
@Miongu You needed to add flags in your update that if they were true, they would continuously move in the correct direction. They become true when you press either A or D. Hope this helps.
Whoops, in the else if in the function 'CheckIfKeysWerePressed', change Keycode.A to Keycode.D instead. Sorry
Yes thank you! It worked perfectly.
I also tried to recreate and experiment on my own and found something similar using bool.
public float forwardForce = 2000f; public float sidewaysForce = 1000f; bool movement;
if (Input.GetKey("d")) { movement = true;
}
if (movement == true)
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, Force$$anonymous$$ode.VelocityChange);
}
if (Input.GetKey("a"))
{
movement = false;
}
if (movement == false)
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, Force$$anonymous$$ode.VelocityChange);
}
It's quite simple and looks messy, but it works the same way.
Thanks again for your help!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Deteriorating force on object? 0 Answers
Why the text and the UI code is can't applying 0 Answers
Changing text values every time you instantiate the button 1 Answer