Stopping my player from moving when hitting a wall.

Simple question, but really couldn’t find the answer to.

I just want my player to stop moving once it hits wall.
What I tried was adding both a Box Collider and a Rigidbody on both objects.
Instead of the wall stopping the player from moving, the wall just gets pushed by the player.
I tried increasing the mass of the wall but now my player is getting bounced back by the wall, or sometimes the player goes right through the wall.
I’ve tried adjusting the numbers of both objects in several different ways and both of them just keeps going crazy and flipping out around the map.

Is there a simple way of making this without any of the unintended complicated physics stuff happening?

Just a simple ‘stop’, no sliding bouncing friction complex physics or anything.

Oh and this is for a Top-Down 3D game.

Thanks! :slight_smile:

Hello Leroterr

You don’t need to increase mass.
Just add rigidbody to your player and collider.
and add collider to wall. and put script on player which detects collision and perform action using that.

//This is for 3D game in C#

void OnCollisionEnter(Collision collision) 
{
        if(collision.gameObject.name == "YourWallName")  // or if(gameObject.CompareTag("YourWallTag"))
        {
            		rigidbody.velocity = Vector3.zero;
        }
}

OnCollisionEnter doc

A surprising good method is actually moving your move script to a FixedUpdate and using rigidbody.position.

try this

void FixedUpdate()
{
    float x = Input.GetAxisRaw("Horizontal");
    float z = Input.GetAxisRaw("Vertical");

    playerRb.position += z * transform.forward * Time.deltaTime * speed;
    playerRb.position += x * transform.right * Time.deltaTime * speed;
}

Make the wall’s rigidbody Kinematic. Then make sure the player’s collider doesn’t have a bouncy PhysicsMaterial.

it works perfectly :slight_smile:

void LateUpdate()
	{
		//fix flying if is blocked on jump
		if(charController.velocity.y == 0)
		{
			moveDirectionSmoothly.y = charController.velocity.y;
		}
			
		//fix "walk stopped" (stop the character if it is blocked by a wall)
		if(charController.velocity.z == 0)
		{
			moveDirectionSmoothly.z = charController.velocity.z;
		}
	}

I have the same question, only with a 2D object. Any advice?
Thanks in advance.