- Home /
How can I make it so that my player only moves if there is no wall in the direction he wishes to move?
Hi guys. Long story short, I am working on a 3rd person game. The character rotates to look at the mouse, and moves up, down, left, and right while doing this. He is a rigidbody. Now whenever he collides with a wall object he jitters and bounces off it like crazy for as long as I am pressing the button. I don't want this. I simply want him to stop. So what I'd like to do is somehow set it up so that player input will only move him in a certain direction if there is nothing blocking him.
Sounds easy at first glance, but the fact that he rotates around the Y axis to look at the mouse makes it much more complicated.
I would appreciate any help on this. Thank you!
Answer by graslany · Sep 03, 2013 at 03:26 PM
You could use some Ray/Sphere/Capsule cast to detect whever there is a wall on the way of your player. See for example the documentation of Physics.CapsuleCast, which may be what you need for a character.
Note : These collision detection functions can selectively ignore some objects when looking for collisions based on the layer they belog to.
Your answer
Follow this Question
Related Questions
Prevent random movement allowing movement through obstacles 1 Answer
Enemy got lit by flashlight detection 1 Answer
Use of an AI script, and having it NOT go through walls using a character controller 0 Answers
Multiple/Different footsteps sounds (solved) 2 Answers
My Raycast acts like it's hitting something when it isn't 0 Answers