When my character gets into a vehicle I need to get the new parent(the vehicle) as my public/private Transform.
The thing is that the player is actually the head bone in the biped and its way deep in there lol.
So how to get the upper most parent which is the transform of the vehicle gameobject that has become the parent of the player? I can’t figure it out…
The way I am doing it only gets the parent of the head bone which is the neck.
Code:
public Transform vehicle;
public Transform player;
if(isInCar == true){
vehicle = player.parent.gameObject.transform;
}
public Transform vehicle;
public Transform player;
if(isInCar == true)
{
vehicle = player.parent;
do
{
vehicle = vehicle.parent;
}
while(vehicle.parent != null);
}
I think that would work if you are sure that the vehicle has no parent transform but I would say that it’s not the right way to do it. It’s highly recursive and thus I wouldn’t recommend doing that in the gameplay loop. It would probably be better to know your vehicle before (for example, when you enter the vehicle, you know which vehicle you’re jumping in) and assign it directly.
Also please note : Transform.parent returns a Transform object.
Transform.parent.gameobject.transform is redundant and useless, you should use Transform.parent directly.