- Home /
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! :)
is your wall a 3d cube or is it a plane? also make sure is$$anonymous$$inematic
is checked on the wall
I heard from some of your replies to the answers you got, that your Player Character is jittery when colliding with the wall. $$anonymous$$aybe, you should should change the ,,Collision Detection" in your Player's Rigidbody from ,,Discrete" to ,,Continuous".
If you want an explanation of this: There are 2 Collision Detection modes, with ,,Discrete" being the default, and it's accurate enough for most situations. But sometimes, you need more accurate results at the expense of a bit more processing power, and this is what the ,,Continuous" Collision Detection $$anonymous$$ode is for.
Answer by yashpal · Oct 25, 2014 at 06:09 AM
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;
}
}
$$anonymous$$y player starts to get "fidgety/jittery" or "shaky" once it hits the wall and me still pressing the forward button. It's like it's still forcing itself to go through the wall (I am, but I don't want the character to act jittery while doing it), then bouncing back a little, then going in again.
I'm also using transform.translate to move my player.
@bubzy just show current way to solve this problem. i think You don't want your walls are moving (means it's static). and jittery effect. you just want to is$$anonymous$$inematic in rigidboady. or don't need to add rigidbody it walls.
This is a good way to do it! although i have found it works a lot better when using raycasts, boxcasts e.t.c.
Answer by r3flx · Dec 13, 2017 at 11:12 AM
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;
}
$$anonymous$$oving it to FixedUpdate really helped! Thanks a lot!
Answer by JoshDangIt · Jun 23, 2016 at 08:23 PM
Make the wall's rigidbody Kinematic. Then make sure the player's collider doesn't have a bouncy PhysicsMaterial.
Answer by Maginix · Jun 24, 2016 at 05:01 AM
it works perfectly :)
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;
}
}
Answer by Pathojen · Mar 23, 2019 at 07:50 PM
I have the same question, only with a 2D object. Any advice? Thanks in advance.
@Pathojen you could add some drag. This way, when the rigidbody 2D hits the wall, it may bounce back but its velocity is dramatically reduced over time until it comes to a stop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
float speed = 4f;
Vector3 moveLeft = new Vector3(-1, 0, 0);
Vector3 moveRight = new Vector3(1, 0, 0);
Vector3 moveUp = new Vector3(0, 1, 0);
Vector3 moveDown = new Vector3(0, -1, 0);
void FixedUpdate()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += moveLeft * Time.deltaTime * speed; //x,y,z
}
//add code for moving right, up, down
}
}
FixedUpdate worked for me in 2D. I gave the player a Rigidbody2D and CircleCollider2D. I gave the wall a BoxCollider2D. The player collider HAS to be a CircleCollider2D (not BoxCollider2D) with this code, or you get a weird glitch where the player can get caught on the corner of the wall and rotate. The other way to fix the glitch is to use a BoxCollider and check "Freeze Rotation Z"