Parenting Unparenting

How does one go about parenting/ unparenting in C# game objects such as a character geting in and outt of a vehicle?

.transform.parent = XY; :slight_smile:

thats how it would mmove , but id assume you wouldnt want the character to be parented to the car all the time

only while he is sitting in the car.
as soon as he isn’t you would unparent him

the parenting is just to have him move along the parent without massive wasted performance on physics to do it twice (once car - once body)

thats what im asking how do i parent him to the car?

explained in the second posting.

transform.parent is the property to set his parent transform :slight_smile:

ok i think i understand now. tranform1.parent = transform2

transform1 would be the parent which in this case is the car, and transform2 is the child or player. That right?

wrong exactly the opposite :slight_smile:

you want to set the parent of the child to the parent. you don’t want to set the parent of the parent to the child :slight_smile:

ok thanks

Ok so let me take another shot at this.

DriverSeat/PassengerSeat.parent = Player

or is it

Player.parent = whatever seat?

// make it a child of the current object
cameraTransform.parent = transform;

so translates to player becomes child of car

then

// Detaches the transform from its parent.
transform.parent = null;
2 Likes

so in short my second choice. thanks :P.

Also how do i keep the player falling through the terrain if the player gets out on a slope?

And how would you check to see if the player is at a door of the car or the back to hop on a turret?

first one you can limit the slope angle (max slope on the FPSWalker script iirc) in effect preventing the player from getting too close…or use collision meshes to prevent him getting there.

if he needs to go onto the slope I’d suggest getting Locomotion

Very good for what you’re asking for.

For the vehical issue create a couple of spheres, 1 near the door, 1 near the turret and set them to unrendered, and use em as triggers based on their collision meshes which activate your climb in/use turret script depending on which is activated.

I was thinking about using colision meshes to see if the player is interceting it and then if he is he can press the e button to get in

That works or you could always raycast from the player to the vehical and use the distance from door or turret, the shortest being the one to use on E key, which only activates when either of them is within a specified distance.

Ok heres my first attempt

	bool Riding = false;
	public GameObject Seat;
	public GameObject PC;
	
	Transform PCT;
	// Use this for initialization
	void Start () 
	{
		PCT = PC.transform;
	}
	
	// Update is called once per frame
	void Update () 
	{
		
	}
	void OnControllerColliderHit(ControllerColliderHit Hit)
	{
		if(Hit.gameObject == Seat  Input.GetKeyDown("Action"))
		{
			PCT.parent = Seat.transform;
		}
	}

I assume that the OnControllerColliderHit is called automaticly when someting collid right?

any ideas why its not working?

Can you explain what exactly is going wrong?

thats just it its not working not trying to be a smart ass but theres no errors or anything.Ive attached it to the body, and or the character. I even put a public bool ontop of that to turn true when it collided nuthing ever happened.

You can check if the parenting has worked using the editor. Run the game and play it through until the point where the player is supposed to be parented to the vehicle. At that point, go back to the scene view and look for the vehicle in the Hierarchy panel. If you open the little triangle, you should find that the player is now a child object of the vehicle. If you move the vehicle manually in the editor, you should find the child object moves around with it unless it has a script preventing this from happening. Generally speaking, you should disable any independent motion of the player while it is parented to another object.