x


Input when not in contact with floor

Im making a skateboarding game at the moment for a college project, i have a couple of inputs to control and flip my board, but i dont want to be able to use the input to flip my board while in contact with the floor but i cant figure out the script to do this, i have the script to flip the board but i only want to be able to use it once my skateboard is in the air. would be much appreciated if anyone could help.

more ▼

asked Jun 12 '10 at 07:49 PM

Ali gravatar image

Ali
2 2 2 3

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You will need to determine whether or not your skateboard is touching the ground.

private groundHitCount : int = 0;

function OnCollisionEnter (collision : Collision) {
    // Determine if it's a ground hit (using tags or vertical raycasting)
    ++groundHitCount;
}

function OnCollisionExit (collision : Collision) {
    // Determine if it's a ground hit (using tags or vertical raycasting)
    --groundHitCount;
}

function AmIGrounded () {
    return (groundHitCount > 0);
}

function Update () {
    if (AmIGrounded()) {
        // Ground input here
    } else {
        // Air input here
    }
}

Making this up as I go but hopefully it at least gets you started.

more ▼

answered Dec 01 '10 at 10:32 AM

Rennat gravatar image

Rennat
664 5 8 18

(comments are locked)
10|3000 characters needed characters left

How to properly answer this will depend on how you set up everything else. If you're using a character collider for movement, the FPSWalker script has an example of how to determine if you're on the ground (off the top of my head, it's something like checking to a bit flag off the return value of .Move()). If you're using wheel colliders on your board, you can use the isGrounded property to just ask if it's on the ground or not.

One thing you could do regardless of anything else, is every frame do a Physics.Raycast to determine if you're on the ground or not. Trace from the player or board or whatever downward, and see if you hit the floor. If you make your ray length pretty small (say, the height of a skateboard wheel), then the question simply becomes "did raycast hit anything".

I'm assuming you can figure out how to turn off your in-air trick input once you can answer the question "am I on the ground".

more ▼

answered Jun 12 '10 at 08:14 PM

Tetrad gravatar image

Tetrad
7.2k 27 37 89

Thankyou for replying but my in-air trick isnt actually an animation, its done by just transform rotate if you know what i mean. so i was wondering if there is anyway to script it so it would be like, if in contact with collider/rigidbody input false?
haha i have no idea :P

Jun 12 '10 at 08:25 PM Ali

Well just use what I talked about, and wherever you do whatever your trick is, just wrap that with if( IsPlayerInTheAir ).

Jun 12 '10 at 09:05 PM Tetrad
(comments are locked)
10|3000 characters needed characters left

If you are using a character controller you can structure your controller.Move call to be like this:

CollisionFlags flags = controller.Move(velocity * Time.deltaTime);
if ((flags & CollisionFlags.Below) != 0)
    grounded = true;
else
    grounded = false;

grounded will be true when the character is in contact with the ground.

Alternatively, there is also a characterController.isGrounded property, however some of Unity's resources show that the characterController's built in properties can be unreliable at times (namely the scripts in the Character Animation Example Project).

more ▼

answered Jun 12 '10 at 08:24 PM

JonManatee gravatar image

JonManatee
251 7 8 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5071
x2488
x48

asked: Jun 12 '10 at 07:49 PM

Seen: 1561 times

Last Updated: Jun 12 '10 at 08:14 PM