Object won't fall when I apply horizontal velocity and is colliding with wall.

Hi there.

Im currently trying to create a simple 2d platforming game, and im having a little trouble with my character when he collides with another object mid-fall/mid-jump. He is jumping, falling and walking sideways like i want him to. However, whenever he jumps into a wall or falls into a wall, if I hold down the ‘walk left’ or ‘walk right’ button (i.e. keep trying to get him to walk sideways into that wall), he freezes in mid air. Could anyone tell me how I might fix this?

Here is some of my script (Javascript):

    public var horizontal_speed : float = 5.0f;
public var jump_Power : float = 15.0f;

private var directionFacing : int = 1;
private var isJumping : boolean = false;

public function Update () : void {

 //Horizontal Movement
 if (Input.GetButton("Horizontal")) {
 // for directionFacing variable -----
 //            1 = character is facing right
 //            -1 = character is facing left
 directionFacing = Input.GetAxis("Horizontal") < 0 ? -1 : 1;
 rigidbody.velocity = new Vector3((directionFacing * horizontal_speed), rigidbody.velocity.y, 0);
 }
 
 //If character isn’t jumping
 if (!isJumping) { 
 //Jump
 if (Input.GetButton("Jump")) {
 isJumping = true;
 rigidbody.velocity = new Vector3(rigidbody.velocity.x, jump_Power, 0);
 }
 }
}

Please note: I later on in this script have a section where “isJumping” is turned back to being false, but it involves a lot of raycast detection stuff that i dont think is relevant. If however you would like me to post that as well, Im more than happy to do so.

Currently the character has a box collider (not trigger) and a rigidbody. It is using gravity and ‘is kinematic’ is off. Constraints are set to freeze all rotation and z translation. Mass = 1. Walls and floor are also using box colliders (not trigger) - no rigidbodies on these.

I’m assuming when I hold down left or right the amount of velocity i am applying to the character is overpowering the effect of gravity, but i dont know how to stop this effect (plus it doesnt make sense to me that this should happen).

Can anyone please help? I dont mind if someone can suggest a C# solution as well, as it looks like it should be a fairly simple issue.

a simple solution:

  • make a Physic Material and make it’s friction to zero
  • assign it to wall’s colliders

a complex solution

  • don’t assign rigidbody.velocity directly. change it adding force
  • use FixedUpdate to interact with physics
  • detect collisions (OnCollisionEnter method) and possibilities to adding forces based on these collisions