Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Checkground function for 3d sidescrolling platform game is killing me.

Discussion in 'Scripting' started by wdwmkr123, Dec 29, 2015.

  1. wdwmkr123

    wdwmkr123

    Joined:
    Dec 27, 2015
    Posts:
    2
    Alright, im very sorry if this is just straight up a noob question but I am just starting out and I have spent the past week consuming every tutorial I can on the web on scripting and how to get started but this one part is just.... not... working.

    So I have a rolling cylinder as my player char, I've got it buzzing left and right happily, not chopping through the floor or freaking out and going interstellar when I hit jump like it used to, all that fun stuff. Now all I want to do is attach a simple groundcheck to it so that it doesn't fly. Here's my code:

    Code (CSharp):
    1. {
    2.  
    3.     public float run = 15;
    4.     public float jump = 5;
    5.     public float grounded;
    6.  
    7.     void OnCollisionenter(Collision other)
    8.     {
    9.  
    10.         if (other.gameObject.layer == 8)
    11.         {
    12.             grounded = 1;
    13.  
    14.         }
    15.     }
    16.  
    17.     void OnCollisionExit(Collision other)
    18.     {
    19.         if (other.gameObject.layer == 8)
    20.         {
    21.             grounded = 0;
    22.         }
    23.     }
    24.  
    25.     void Update ()
    26.     {
    27.  
    28.         Rigidbody cyl = GetComponent<Rigidbody> ();
    29.         Vector3 curr_velocity = cyl.velocity;
    30.         if (Input.GetButton ("Horizontal"))
    31.         {
    32.            
    33.             cyl.AddForce (Vector3.right * Input.GetAxis ("Horizontal") * run);
    34.  
    35.         }        
    36.  
    37.         else
    38.         {  
    39.            
    40.             Vector3 multiplier = new Vector3(0.97f,1,1);
    41.             cyl.velocity = Vector3.Scale(curr_velocity, multiplier);
    42.             if(Mathf.Abs(curr_velocity.x) < 0.001)
    43.                
    44.             {
    45.                
    46.                 cyl.velocity = Vector3.zero;
    47.                 cyl.angularVelocity = Vector3.zero;
    48.             }
    49.         }
    50.  
    51.         if (Input.GetButtonDown ("Jump"))
    52.         {
    53.            
    54.             cyl.AddForce (0,(grounded * jump),0, ForceMode.Impulse);
    55.  
    56.         }
    57.     }
    58. }
    As you can see im using the rigidbody's OnCollision code to send a simple binary, 1 if it is grounded, 0 if it's not grounded. I've used the OnCollisionStay in an "if/else" statement I've fiddled with the collisions sleep/wake stuff to make it continuous. And it never changes on the fly. It's always just stuck in either 1 or 0. meaning The player either spends the game flying everywhere happily or not able to jump at all. neither of those are platformers.

    The other thing i've seen is using a raycaster. I set one of those up before when I first started following a youtube tutorial pretty much 1 to 1, I get the gist of it, my only concern is that this is a rolling cylinder, not a box or a square, I imagine I'd either need a set of rays coming out every so many degrees along the cylinders axis or a seperate raycasting "foot" that is tied to the player. But I have no idea how to make that and I can't find tutorials on those anywhere.

    Any of you have any ideas? I'm not looking to be led by the nose I just need to know where to begin, i've been at this particular roadblock for three days now.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,745
    The e in your "OnCollisionEnter" is not capitalized, and needs to be.
     
  3. wdwmkr123

    wdwmkr123

    Joined:
    Dec 27, 2015
    Posts:
    2
    Holy...Holy S***.... I am so sorry for posting this now. this was such a stupid mistake. I was up until 3:30 am digging through every tutorial I could find trying to figure this out this was straight up breaking my brain as to why this wasn't working and was such an idiotic mistake.
    Again, so sorry for this.
     
    StarManta likes this.