- Home /
checking if another object is grounded
i seem to be having trouble checking from another object if the player is grounded. I have a Character Controller on the player and tryed to get the access it as a component, Im probably missing something simple and cant see it.
var Player : GameObject; var FollowY: boolean = true; var FollowX: boolean = true;
function Start () { Player = GameObject.Find("Player"); var charController : CharacterController = Player.GetComponent(CharacterController); }
function Update () { if(FollowY == true) { if(charController.isGrounded == true) { transform.position.y = (Mathf.Lerp(transform.position.y, Player.transform.position.y, Time.time)); }
}
if(FollowX == true)
{
transform.position.x = Player.transform.position.x;
}
}
A variable declared inside a function is temporary, and is destroyed when the function returns. You should declare a member variable ins$$anonymous$$d (declared outside any function) - member variables exist during the script life.
var charController: CharacterController; // <- member variable
function Start ()
{
Player = GameObject.Find("Player");
charController = Player.GetComponent(CharacterController);
}
Answer by stingman · May 12, 2012 at 12:08 AM
Try this...
private var Player : GameObject;
var FollowY: boolean = true;
var FollowX: boolean = true;
function Start () {
Player = GameObject.Find("Player");
}
function Update () {
var charController : CharacterController = Player.GetComponent(CharacterController);
if((FollowY) && charController.isGrounded) {
transform.position.y = Vector3(Mathf.Lerp(transform.position.y, Player.transform.position.y, Time.time)); }
if(FollowX)
{
transform.position.x = Player.transform.position.x;
}
}