Search Unity

How do I stop my first person character going through walls?

Discussion in 'Getting Started' started by jamiecropley, Jun 10, 2020.

  1. jamiecropley

    jamiecropley

    Joined:
    Oct 20, 2014
    Posts:
    46
    As per my code at: https://github.com/Some-T/FirstPers...aster/Assets/Scripts/FirstPersonController.cs

    I can't figure out what I am doing wrong in my code, I tried to write a first person controller from scratch.

    The full project can be downloaded from: https://github.com/Some-T/FirstPersonController-CSharp

    My player goes through any wall or object easily despite it having colliders on the objects and a rigidbody and collider on the player.

    I have gone through my code line by line and I cannot work out what is wrong? If someone could please advise me what to change or add to my code to fix this, or / and explain the concept, presumably physics related, as to why this is going wrong please?
     
  2. TomSien

    TomSien

    Joined:
    May 16, 2020
    Posts:
    12
    Hi, can you make sure you are moving your player using rigidbody.moveposition and not transform.translate (as this one ignores physics like collisions)? This should work as long as your player is non-kinematic.
    I think also sometimes, when the speed of objects is too high, it can fail to properly calculate the collision too.
     
  3. jamiecropley

    jamiecropley

    Joined:
    Oct 20, 2014
    Posts:
    46
    I forgot I made this post, since I initially made it my code has now been updated, see: https://github.com/Some-T/FirstPers...5/Assets/Scripts/FirstPersonController.cs#L73 and my main issue is getting velocity movement working, in respect of when my player camera faces a direction, the direction key correlates, e.g. if I push W key the player will move in the direction the camera is facing, at the moment the WASD movement keys go all backwards and such.