Search Unity

Help with Colliders

Discussion in '2D' started by ddripper, Sep 19, 2016.

  1. ddripper

    ddripper

    Joined:
    Sep 19, 2016
    Posts:
    3
    Hello there. I'm a beginner in Unity3D and C#
    So I'm basically trying to recreate Google Chrome's offline Dinosaur game as my semester project and I'm stuck with this particular issue with colliders. I have a separate ground object and a separate player object (dinosaur) . So when spacebar key is pressed, the player object jumps. So I'm trying to figure out a way to prevent key spamming so as to prevent multiple jumps while in air. I thought of adding a separate empty game object with box collider 2d and rigidbody 2d (kinematic option ticked) as children to both player and ground object and write a separate script for collision between player object and ground object. I tested the collision by printing a log in the console and it seems to work but it detects only one collision (the first one) even after multiple jumps.
    Help please ?

    Thank you for your time.
     
  2. Aerin41

    Aerin41

    Joined:
    Dec 24, 2014
    Posts:
    14
    Could you post the code you're using for the player? It'd make it easier to see if there's a problem with the code or if there's something else that needs to be adjusted.
     
    ddripper likes this.
  3. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    Have a look at the unity 2D platformer tutorials. They have a great example on how to do this for 2D, but should work the same in 3D. The basic principle is:
    - Have a boolean to track if the object is jumping
    - When the jump button is pressed, only run the jump method if Jump is false
    - Set jump to true at the start of the jump method
    - Have a collider on your ground objects that when collideded with run a method
     
    ddripper likes this.
  4. ddripper

    ddripper

    Joined:
    Sep 19, 2016
    Posts:
    3
    Sorry for the late reply ! And thank you for your suggestions. I found a different way to get around the issue not long after I posted this thread.
    Code (CSharp):
    1. if ((Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) && Time.time > can_jump) {
    2.             set_flag = true;
    3.             can_jump = Time.time + jump_delay;
    4.         }