Can raycasting help me on this?

well i made a script to define all the animations of the main character, when he falls of a cliff he has to do a big dramtic falling pose… with his arms streched out and all that…

but now, i cant tell unity what is a high cliff, and what is just a stairwell… and everytimes he has do fall, even if it is a small high like steps of a stair… he does aalll that pose of falling the grand kaynon… offcourse the animation is interrupted when he soons touches the floor again… but it just looks bad :confused:

how can i use raycast to tell unity that there is floor right bellow the character, and that he cannot play the big fall animation, just a medium fall animation?

i know nothing about raycasting but i think the main goal is to send another game object to check the area before the character goes right?, i mean… i send a invisible sphere and if it touches the floor quickly it means that he will not fall of a great high

is that it?

can somebody post an example of raycasting?

You have the baisc idea, and what you propose should work, Raycasting just ends a single line (ray, actually, ) from one point out in a particular direction and then reports back what, if anything, it hit.

There are examples in the docs and there are a lot of examples in this forum if you do a search. If you have any specific questions you can post them.

okey… well… one last question

this ray… that is casted… is a gameobject? or is just based in variables?

and if i may… is threre anyway to do what i need without having to use raycast?.. because im been thinking… maybe if i could just do an if statement that checks if the distance between the character and the ground is less than 5 centimeters

i just need to know how to code this, but i know the basic idea behind it

It is not a gameobject. Imagine it as a laser that is fired against the colliders in a layer or scene.

If you have some way to get the height of the ground beneath your character, yes you can just subtract the ground height from the characters height. If you are using Terrain it has a method to tell you the height at a given x,z coordinate.

Have you looked in the docs?

There is an example in there that is pretty much what you need,

you mean this one?

// Raycast up to 100 meters forward

function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
var distanceToGround = hit.distance;
}
}

as i undestand, this sents a ray 100 meters below the character to see if there are coliders right?

then the right move after this would be…

if ( distanceToGround < 5 ) { dont play big fall animation, because is just a small cliff }

else { play big fall animation, anything above 5 is a great cliff }

this?

yup!

didnt work …

but i think i got the spirit of it… im gonna keep trying

its almost working. i mean, sometimes it plays the long fall animation… but when its realy high… then it doesnt play anything and the character keeps walking in the air… weird

EDIT>
i have a simple question about raycasting, whats the comand to cast a ray in the down direction?

i mean… in minus Y direction

Try debugging out your distanceToGround to see how far the ray thinks the ground is.

Debug.Log(distanceToGround)

it might show a number you aren’t familiar with and could be caused by the level having some hidden colliders.

Also, do you have any colliders on your character that might be getting hit? If you do, try setting the starting position to something below the character.

function Update () {
var hit : RaycastHit;
var start : Vector3;

start = transform.position;
start.y -= transform.localScale.y / 2;

if (Physics.Raycast (start, -Vector3.up, hit, 100.0)) {
var distanceToGround = hit.distance;
}
}

Given the code you copied from the docs:

if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {

as the docs say:

So in your case, if your transform it is more than 100 units above the ground, it returns back that it hit nothing (False), since you limited the distance the ray can go to 100. You can set it to a much higer value or just use Mathf.Infinity for endless. Infinity is probably fine here.

Again, from the docs:

click on the Vector3 link to get to:

so if Vector3.up is UP then negative up ( -Vector3.up) is DOWN:

It’s worth your time to really read the docs closely… several times, and click on the links in the docs.

yhea i know, im reading then over and over again to absorb as much of knowledge i can about it … but honestly… i hope this is the first and only time i need to use raycast in my game because i feel like is way out of my league

oh and thanks trooper! for the script references!

now it works perfecly, thank you all