I am working on a 3D Platformer in Unity3D and I need to prevent the player from going through walls. The movement for my player works like this:

bird-eye-view player movement

(A bird-eye-view representation)The areas the player can move a divided into layers. He can slide into the 2nd, 1st , or 3rd layers. The player is not a rigid-body, and is controlled using a character controller. To move the player into these layers I simply increase his Z-Axis. Currently if my player is behind the wall he will slide directly into it. How can I determine if there is a wall to the left of the player in Unity 3D? If so, I simply wont allow the user to move to the layer until they have steered clear of the obstacle. Does anyone have any suggestions of what I should do?


You can use function CharacterController.Move to detect collision to the wall. if you don't want to use Move then you can cast a ray with Raycast to check if wall close enough to stop your character or let him move ;)

  • \$\begingroup\$ Thanks. I ended up going with CharacterController.Move function MoveTo(newPos : Vector3, controller : CharacterController) : IEnumerator { vertMoveDirection = newPos - transform.position; controller.Move(vertMoveDirection); } However I want the movement to be smooth. Is there anyway I can do this in this function? \$\endgroup\$
    – Grey
    May 25 '13 at 22:19
  • \$\begingroup\$ what's wrong with movement ? \$\endgroup\$
    – Artyom
    May 25 '13 at 22:50
  • \$\begingroup\$ It sort of teleports. Anyway I'll figure it out :) \$\endgroup\$
    – Grey
    May 25 '13 at 23:20
  • 1
    \$\begingroup\$ Vector3.lerp could help ;) \$\endgroup\$
    – Artyom
    May 25 '13 at 23:25
  • \$\begingroup\$ Ended up using raycasting =) \$\endgroup\$
    – Grey
    May 26 '13 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.