Using AddForce for horizontal movement doesn't quite work the way I want it to (2D)
I'm working on a 2D-Platformer and my character has a punch attack that is supposed to propell him forward a little bit in whatever direction he's aiming at. This works fine when I punch vertically, however whenever I want to punch horizontally, my character just jerks around on the floor. (He basically just teleports a short distance and then abruptly stops)
What I want to achieve is to use add force to make him slide for a short distance instead of having this abrupt movement.
Here's how I'm applying force right now. This code is executed whenever I press my fire button.:
if (Input.GetButton("FireX"))
{
compRigidBodyPlayer.AddForce(new Vector2((force * dir.x), (force/2 * dir.y)));
//compRigidBodyPlayer is the rigidbody component of my player gameobject.
//dir is a Vector2 that contains the direction I'm aiming at
//(x and y can either be 1, 0 and -1)
}
How can I make my player character slide horizontally when using AddForce? Or should I use an entirely different way?
Answer by Jayrab · Apr 11, 2018 at 08:09 AM
Found my answer on here
The problem was that my CharacterController script would re-adjust the velocity of my character in FixedUpdate()
rb.velocity = new Vector2(Input.GetAxis("Horizontal") * runSpeed, rb.velocity.y);
Since there's not necessarily any input when I punch, the velocity would immediatly be set back to 0.