Is there a cheap way to sense if my gameObject isn’t touching anything? For example, if it fell off a ledge. I can’t use relative.Velocity in this case.
Hi Hawken,
2 methods to do this… first one is to use the OnCollisionStay function to override a default boolean, eg
var touching : boolean = false;
function FixedUpdate(){
touching = false;
}
function OnCollisionStay(){
touching = true;
}
This will basically check every physics frame to see if the object is colliding with anything, if it is touching will be true if not it will default to false. I’ve seen this used a lot to check if a character is grounded - however it can sometimes flip states (not usually noticeable)
The second method is to do a raycast which would be a preferable option IMHO:
function FixedUpdate{
var ray : Ray
if (Physics.SphereCast (transform.position, (RADIUS, FLOAT), ray, (DISTANCE FLOAT),(LAYER MASK)) {
touching = true;
}
else{
touching = false;
}
}
Note that both of these approaches will test against ANY object with a collider on it, you may find that you want to check against a gameObject tag or use layermasks as well
http://unity3d.com/support/documentation/ScriptReference/Physics.SphereCast.html
Thank you this was most useful. I now have this tailored to respawn at the same space as where the player fell off a ledge. Of course a whole heap of tweaking needs to be done (finding the angle of direction and pulling the respawn back a bit for example so they don’t respawn into empty space) - this got me on the right track.
I hope to repay the favor when my noob seal is broken.
Thanks you very helpful!