Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Fast FPS controller phasing through objects when run through?

Discussion in 'Physics' started by TlemoDAD, Nov 10, 2019 at 11:06 PM.

  1. TlemoDAD


    Dec 22, 2018
    I am in the process of making a Unity game, and I want to make an FPS controller that moves quickly (sort of like in Doom and other ID Software games). Though one problem I face is that collisions with the said controller are ineffective; the player may run through the object (in this case, a palm tree) and go within the geometry of it.
    I am not using any physics materials on the controller or palm trees. The tree's mesh collider isn't convex (otherwise collision would occur in the space between the bottom of the stump and the leaves of the tree). I have put two colliders inside of the character controller (a capsule collider and a box collider following various attempts to fix the collision). Both the capsule and box colliders have a height of 2 units, and a radius of 0.5 (on the box collider it would have a width and length of 1). Could someone help me with this, please?
    A height comparison between the tree and the player.
    The properties of the player's rigid body and controller script.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    5. public class characterController : MonoBehaviour
    6. {   //eat beans you piece of code
    8.     private float speed = 10.0f;
    9.     public float WalkSpeed = 10f;
    10.     public float sprintSpeed = 15f;
    11.     private Rigidbody rigidBody;
    13.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         rigidBody = gameObject.GetComponent<Rigidbody>();
    18.         Cursor.lockState = CursorLockMode.Locked;
    19.     }
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (Input.GetKey(KeyCode.LeftShift))
    25.         {
    26.             speed = sprintSpeed;
    27.         }
    28.         else
    29.             speed = WalkSpeed;
    31.         float translation = Input.GetAxis("Vertical") * speed;
    32.         float straffe = Input.GetAxis("Horizontal") * speed;
    33.         translation *= Time.deltaTime;
    34.         straffe *= Time.deltaTime;
    36.         transform.Translate(straffe, 0, translation);
    38.         if (Input.GetKeyDown("escape"))
    39.             Cursor.lockState = CursorLockMode.None;
    40.     }
    41. }
    Code to the character controller script.
    And the link to the tutorial that helped me build the controller:

    I'm somewhat of a beginner, so expect errors. Always remind me when I'm forgetting something!