Search Unity

[Solved]What am I doing wrong with my platform colliders ?

Discussion in 'Physics' started by Aeskyphaz, Jul 25, 2017.

  1. Aeskyphaz

    Aeskyphaz

    Joined:
    Jun 2, 2017
    Posts:
    52
    Hello there
    , I recently started the long journey of learning C#, and after a few hours of watching and reading tutorials and digging into the Standard Assets controller scripts, I came up with my very first code prototype, which is supposed to be a 2.5D platformer character controller. It works decently well, the character can move left, right and jump. However, as far as my jump system is concerned, I included a bool called isGrounded that is required for the player to jump, and is set to true when a collider on the character encounters the ground. It works well, however, when I duplicate the cube that I use as a protoype floor to extend the initial platform, this second section does not trigger the collider ! I can't figure out why... Here's the code code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.  
    8.     Rigidbody Rgbd;
    9.     Animator animCtrl;
    10.     CapsuleCollider p_Collider;
    11.  
    12.     public float speed;  //setting up floats
    13.     public float altitude;
    14.     public float jumpForce;
    15.     public float moveForcef;
    16.     public float hInput;
    17.  
    18.     public bool isGrounded;  //setting up bools
    19.     private bool isRunning;
    20.     private bool isJumping;
    21.     private bool isCrouching;
    22.  
    23.     void Start () //Au démarrage...
    24.     {
    25.         Rgbd = GetComponent<Rigidbody> (); //.cache rigidbody and animator data
    26.         animCtrl = GetComponent<Animator> ();
    27.         isGrounded = false;  //by default, not grounded
    28.     }
    29.  
    30.     void OnTriggerEnter ()  //on platform collide -> grounded
    31.     {
    32.         isGrounded = true; //grounded on
    33.         Debug.Log ("Trigger enter");
    34.     }
    35.  
    36.     void OnTriggerExit () //opposite
    37.     {
    38.         isGrounded = false;
    39.         Debug.Log ("Trigger leave");  
    40.     }
    41.  
    42.     void FixedUpdate ()  //each frame
    43.     {
    44.         float hInput = (Input.GetAxis("Horizontal"));
    45.         Rgbd.AddForce (hInput * Vector2.right *moveForce); //when input, apply force
    46.         if (isGrounded && Input.GetKeyDown(KeyCode.Space)) //if space pressed + grounded
    47.         {
    48.                 Rgbd.AddForce (jumpForce * transform.up);  //apply force up
    49.                 Debug.Log ("Force applied");
    50.         }
    51.  
    52.         else //and if not grounded
    53.         {
    54.             //nothing happens
    55.         }
    56.     }            
    57. }
    I apologize in advance for how clumsy this code probably is, since this is my first attempt ever... but if anyone has an idea why the collider doesn't happen to work out of the initial platform, I'd be relieved ! Thank you very much in advance

    NB : I already set up the colliders and rigidbody
     
  2. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
  3. Aeskyphaz

    Aeskyphaz

    Joined:
    Jun 2, 2017
    Posts:
    52
    Worked like a charm ! Thank you very much