Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Character falling through the ground. Please help

Discussion in 'Scripting' started by Casper_Stougaard, Jul 31, 2013.

  1. Casper_Stougaard

    Casper_Stougaard

    Joined:
    Jul 18, 2013
    Posts:
    60
    Hello.

    I have been trying to create a script that is supposed to make my character move and jump, and apply gravity. However, when i start my game, my character controller just falls through the ground.
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var speed : float = 3.0;
    5. var gravity : float = 2.0;
    6. var jumpSpeed : float = 300;
    7. private var moveDirection : Vector3 = Vector3.zero;
    8.  
    9.  
    10. function Update () {
    11.     Move();
    12.  
    13.     var controller : CharacterController = GetComponent(CharacterController);
    14.  
    15.     if (Input.GetButtonDown("Jump"))
    16.         if(controller.isGrounded) {
    17.         moveDirection.y = jumpSpeed;
    18.         }
    19.    
    20.        
    21.     transform.position.y -= gravity * Time.deltaTime;
    22.     controller.Move(moveDirection * Time.deltaTime);
    23. }
    24.  
    25.  
    26. function Move() {
    27.     if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.2) {
    28.         if (Input.GetAxis("Horizontal") > 0.02) transform.eulerAngles.y = 180;
    29.         else if (Input.GetAxis("Horizontal") < -0.02) transform.eulerAngles.y = 0;
    30.         transform.Translate(Vector3.forward * Mathf.Abs(Input.GetAxis("Horizontal")) * Time.deltaTime * 3.5);
    31.     }
    32. }
    33.  
    The components on my character are: A Character Controller, A Box Collider, My Movement Script, Another script used in my game, A Mesh Renderer and a Cube (Mesh Filter).

    It all works when i put in this code:
    Code (csharp):
    1.  
    2.  
    3. var speed : float = 10.0;
    4. var gravity : float = 20.0;
    5. var jumpSpeed : float = 8.0;
    6. private var moveDirection : Vector3 = Vector3.zero;
    7.  
    8.  
    9. function Update () {
    10.     var controller : CharacterController = GetComponent(CharacterController);
    11.     var forward : Vector3 = transform.TransformDirection(Vector3.back);
    12.     var curSpeed : float = speed * Input.GetAxis ("Horizontal");
    13.  
    14.  
    15.     controller.SimpleMove(forward * curSpeed);
    16.  
    17.     if (Input.GetButton ("Jump"))
    18.         if(controller.isGrounded){
    19.             moveDirection.y= jumpSpeed;
    20.     }
    21.    
    22.     moveDirection.y -= gravity * Time.deltaTime;
    23.     controller.Move(moveDirection * Time.deltaTime);
    24.    
    25. }
    26.  
    I've been trying to switch out different pieces of code, to make my original script Work, without any luck though.
    I tried commenting out different lines, to see it would start colliding with the ground, but still no luck.. I don't know what to do.
     
    Last edited: Jul 31, 2013
  2. groovounet

    groovounet

    Unity Technologies

    Joined:
    Jul 15, 2013
    Posts:
    16
    Hi,

    My guess is that your scripts are not the issue but I might be wrong.

    Once you apply gravity to your character, the ground needs to have a collider for the physics between the character and the ground to be applied.

    If it goes through, maybe you have disabled the ground collider (which is created enabled by default).

    To check that:
    - Click on your ground object.
    - In the inspector, look at the section Box Collider.
    - Just next to the title "Box Collider" there is a check box. If isn't unchecked, the collider is disabled. Check it and that will work.

    Christophe
     
  3. IroncladEarl

    IroncladEarl

    Joined:
    Jul 3, 2013
    Posts:
    87
    First thing you need to do is place your character above the ground. Sometimes when it is clipping into a plane, it fals through.

    If thats not the issue, then does your character have a rigidbody?
     
  4. Casper_Stougaard

    Casper_Stougaard

    Joined:
    Jul 18, 2013
    Posts:
    60
    @Christophe
    The ground does have a mesh collider. I tried changing to a box collider just for testing. I use a plane for my ground. The mesh collider is not disabled, and it is not set to "is trigger".

    @Saladon
    My character is placed above the ground, and the charactercontrollers "capsule" is correctly placed on my character object.
    No, my character does not have a rigidbody.

    My Whole point with this was, to avoid using a rigidbody because it gave me some other issues that i found easier to fix by using a character controller instead :)
     
  5. IroncladEarl

    IroncladEarl

    Joined:
    Jul 3, 2013
    Posts:
    87
    I had the same problem with my artificial gravity pulling me through the ground until I added a rigidbody to my character. I just constrained the x and z rotation, and any other axes I wanted manual control over. The character controller is a collider component which means it will still work with a rigidbody.