Activate script when child is present

I’m messing around with something here.

I start out with a script disabled and want it enabled when it has a specific child.

function Start(){
this.enabled=false;
if(transform.Find("Cube/FirstPersonPlayer")){;
this.enabled=true;
}
}

//Adds a force upwards in the global coordinate system
function FixedUpdate () {
if (Input.GetKey ("w"))
rigidbody.AddForce (Vector3.up * 10);
}

It however does not enable. It will if I remove the

if(transform.Find("Cube/FirstPersonPlayer")){;

I got that line from the scripting reference and just changed the path to reflect the path when the player is parented. It is correct, in the Heirarchy it is:

Cube

FirstPersonPlayer.

Am I even remotely close to making this script activate when I parent? The script is on “Cube” and contains the movement information for it.

Nevermind on the activate script part,I got that now.

I just have to figure out how to get the children to move with the parent. I’m using applyforce to the parent and only the parent is moving.

I swear I had seen something that specified allowing you to move the parent and all of its children in the Unity scripting reference documentation.

I can’t seem to find it anymore, anyone happen to know what it was? I’ve searched under tranform, move, translate, child, parent, etc.

Have you got rigidbody components on the child objects? You should only have one on the parent.

I had one rigidbody on the player character and one on the vehicle. I turned off collisions and such for the player, made it kinematic so it wouldn’t fall out of the vehicle(was told to do that).

Ugh, guess I’m not as close as I thought I was.

EDIT I tried removing the rigidbody and just manually putting it in the car to test movement. It still leaves the player beind, moving only the car. cry

If the child is parented to the vehicle’s transform (by setting the child’s transform.parent to the vehicle’s transform), then the player should move with the vehicle. However, this won’t happen if the player has its own rigidbody enabled - the player’s rigidbody will have inertia, but won’t be pushed by the vehicle’s colliders. You should set the isKinematic flag on the player’s rigidbody while it is in the vehicle, that way, it should be a well-behaved child, so to speak.

If you’re sure you are doing all of this, then there would seem to be something else complicating matters. Perhaps you could post the script you are using? The significant parts are the bit where the player is parented to the vehicle and where the forces are added to the vehicle’s rigidbody.

Here is what I’m using to parent, the script is attached to the player and is activated when within a certain distance AND a key is pressed.

var hit : RaycastHit; 


function Update () { 



if (Input.GetKey ("r")  Physics.Raycast(Camera.main.ViewportPointToRay(Vector3(0.5, 0.5, 0.0)), hit,5) ) 
{ 

if (hit.rigidbody  hit.rigidbody.CompareTag ("Vehicle")) { 
GetComponent(CharacterController).detectCollisions = false;
rigidbody.isKinematic = true;


//Added this just to see if raycast was working 
print ("Raycast has detected a vehicle."); 

print("Disabling Player controls");
PlayerInput = GetComponent(FPSWalker);
PlayerInput.enabled=false;

print("Putting Player in Vehicle");
transform.parent = hit.transform; 

transform.localPosition = Vector3(0,0,0);
}
}
}

This is what I have on the car object, the script is attached to the car. It is just a simple movement to test out the idea, before I fooled with trying to make the control more car like.

// I had this from when I was deciding if I want it to start disabled
// or enabled and be activated by the player when it became a child.
function Start(){
this.enabled=true;
}


//Adds a force on  global coordinate system
function FixedUpdate () {

if (Input.GetKey("w")  GameObject.Find("/Cube/FirstPersonPlayer")){

rigidbody.AddForce (Vector3.right * 10);

print("OK");
}
}

Thanks for taking the time to look at this.

I think I found the problem, or part of the problem.

I tried mounting, then disabling the entire character, but leaving its children disabled.

This allowed the car to take off moving, and took the children with it. Since I had the rigidbody on the char disabled, could the character controller be causing the trouble instead?

The character has one on him. I am using the character from the FPS tutorial, and have modified the FPSWalker script to allow running and stamina drain. I had added the car mounting script to him as well. He has all the weapons and the character controller like in the tutorial.

He also had a rigidbody added so I could use the iskematic line to stop him from falling out of the car.

Should I:

A:Remove the Rigidbody from the char and use the charactercontroller, finding some way to stop it from falling out?

B:Remove the CharacterController and use a Rigidbody instead?

I was thinking maybe the 2 are kind of conflicting when I make it a child?

Could you check few issues: Character scripts fixed update, or update, or OnCollisionStay, or any other regularly called physcis update doesn’t set character position when it’s IN that parent.

I actually figured it out yesterday before I left work.

I noticed if I disabled the FPS Player while he/she/it was in the car, it would drive off with everything in it.

I rememebered the post above about it all should work if the player had a rigidbody, which it did.

I changed a few things out of curiousity, and away the thing drove with the camera, player, his guns, and all that inside.

Addforce adds a pretty realistic deceleration as well, I hadn’t noticed until I was actually in the thing. Thanks for all the advice and suggestions.

Between the part about if it had a Rigidbody it should work, and just dumb luck the idea occured to me.