- Home /
Rigidbody Addforce cancels out Rigidbody velocity..maybe?
Hello everyone,
I like to think of myself of someone who is a little bit above beginner when it comes to coding/scripting in Unity. The problem that I'm having may be simple but I can not find the problem that is causing the headache. I've used Rigidbody.AddForce for causing force to happen when the player jumps and then I use that again to cancel out the initial jump AddForce. (works fine) The problem is once the gameobject is back on the ground, he seems to be moving in "place". I will break down what my set looks like. I apologize in advance if this is too wordy.
Screen0.png ( I did not add image since I'm limited to 2 image attachments)
The GameObject is simply running left and right with a keydown command that I set up in the Update function.
//Apply velocity to the rigidbody if statement is true
if (Input.GetKeyDown(KeyCode.D) && isGrounded == true ) { rbody.velocity = new Vector3(-5f, 0, 0); transform.localRotation = Quaternion.LookRotation(new Vector3(-180f, 0, 0)); }
if (Input.GetKeyDown(KeyCode.D) && inAir == true)
{
rbody.velocity = new Vector3(-5f, 0, 0);
transform.localRotation = Quaternion.LookRotation(new Vector3(-180f, 0, 0));
}
if (Input.GetKeyDown(KeyCode.A) && isGrounded == true)
{
rbody.velocity = new Vector3(5f, 0, 0);
transform.localRotation = Quaternion.LookRotation(new Vector3(180f, 0, 0));
}
if (Input.GetKeyDown(KeyCode.A) && inAir == true)
{
rbody.velocity = new Vector3(5f, 0, 0);
transform.localRotation = Quaternion.LookRotation(new Vector3(180f, 0, 0));
}
Of course I have a keyup command to cancel those out so I will spare adding that info. But the above code is working fine. (This picture also points out the Capsue collider, a "Trigger" collider that I use to detect if the player is on the ground or not. I do have a raycast setup but did not see a difference whether I used that or a "trigger". I will spare the OnTriggerEnter & OnTriggerExit functions since the do not effect the velocity or addforce.
Screen1.png
The player is having negative AddForce effects to cancel out the AddForce that was used for jumping.
//If the player is in the air X amount of time perform gravity force if (airTimer >= 35) { rbody.AddForce(new Vector2(0, gravity * 6));
}
Screen2.png
The GameObject has landed and I'm trying to move it again with rigidbody.velocity but for some reason it seems as if the GameObject is running into a invisible wall.
I'm not sure if the Update function, where I have my keydown command, is not playing nicely with my AddForce function which is called from a public void Jump() function. Once the game object has reached a certain height the AddForce is called in the Update function to cancel out the initial force from the jump function. The Rigidbody.velocity, which is also in the Update function, no longer works. I hope this was not too much detail but I appreciate anyone who can help me get passed this hurdle.