How can I invoke a function the moment that a character controller touches a ground?
Dont know if you’re wanting it to do it when you hit a specific surface, or EVERY time it lands on something…
Attaches to the character controller
This will work EVERY time upon landing after leaving the ground →
var controller : CharacterController ;
var wasAirborne : boolean = false ;
function Start(){
controller = GetComponent(CharacterController) ;
}
function Update () {
GroundCheck() ;
}
function GroundCheck(){
if(wasAirborne){
if (controller.isGrounded){
//!!--This is where your code for calling or doing your
//other stuff after landing would go
//e.g. DoMyLandingFunction();
Debug.Log("We touched down!") ;
wasAirborne = false ;
}
}
else{
if(!controller.isGrounded){
wasAirborne = true ;
Debug.Log("We're airborne!") ;
}
else{
wasAirborne = false ;
Debug.Log("Just walkin around now...") ;
}
}
}
//function DoMyLandingFunction(){ here }
Otherwise, if you're wanting to call/invoke something upon contact with a specific object you could use OnControllerColliderHit or modify the code above to suit your need.