Search Unity

only jumping when in contact with the ground.

Discussion in 'Scripting' started by remz, Aug 31, 2009.

  1. remz

    remz

    Joined:
    Apr 21, 2009
    Posts:
    16
    Hello,

    I'm writing a piece of game where the player controls an object (ball) which is a rigidbody and should be affected by the physics. So I didn't add a Player Controller to the player's ball. The thing is, I'd like to detect wether the ball is "grounded" or not so I know wether it can jump or not. all the examples I've found doing that rely on the PlayerController and its CollisionFlags enumeration (CollisionFlags.Below). Since the PlayerController's source code isn't available and I'm prety new to unity I don't really know how to go about this. any help or hint is more than welcome!


    remz
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If the ground is a flat plane, then you just need to see how far the ball's Y coordinate is above the plane's - if it is greater than the ball's radius, the ball is obviously not grounded. If the ground is not flat, you can add methods called OnCollisionEnter, OnCollisionExit and OnCollisionStay to the ball's script. These register whether the object has made contact, left contact or is currently in contact with something else. You can use OnCollisionEnter and OnCollisionExit to detect when the ball hits or leaves the ground. These methods are passed a Collision object as a parameter which contains information about which object was hit, where the points of contact were, etc.
     
  3. remz

    remz

    Joined:
    Apr 21, 2009
    Posts:
    16
    thanks a lot for the fast reply, I'll look into that!