- Home /
RigidBody.AddForce not working
Hi there! I'm using AddForce to make the character dash forward when the space key is pressed. But when I press the space key the player won't move at all. Does anyone know how to fix the problem? Thanks!`
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Q")) {
dash ();
}
}
void dash ()
{
Vector3 dash = new Vector3 (0, 0, 100);
dash = transform.rotation * dash;
RigidBody rb = GetComponent<RigidBody>();
rb.AddForce (dash);
}`
by the way I'm aware that in the code it is activated by the "Q" key, by that is not the problem I'm having
Answer by Koen-Matthijs · Jan 25, 2017 at 07:46 AM
I can only guess your rotation is (0,0,0) at that point which makes dash (0,0,0). No need to include the rotation at all anyway.
Just use Vector3.forward to specify the rigidbody should move in the direction the transform is facing.
void dash ()
{
RigidBody rb = GetComponent<RigidBody>();
rb.AddForce( Vector3.forward * 100f);
}`
Question: Is 100 enough force to actually move the body?
I would also suggest you use FixedUpdate() instead of Update(), as that's where Physics updates belong (due to the nature of the tick)
Regards
Koen Matthijs
Question: Is 100 enough force to actually move the body?
You just literally saved my day, good sir!