- Home /
Can't get transform of parent!
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;
}
And how/when did you make this vehicle a parent/grandparent/etc. of your character?
I think you should assign your vehicle
variable when actually getting into the car.
Answer by Xelareip · Nov 18, 2013 at 04:36 PM
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.
Doh, lol. ArkaneX was right.
Ok thanks.
I just set the vehicle from the player controller into the camera controller.
Your answer
Follow this Question
Related Questions
Why GameObject.Find() work and parent.transform.Find() doesn't work? 1 Answer
I want to find a transform when an object is instantiated 2 Answers
Make into child of a parent in c# 1 Answer
How can I parent the player to another GameObject (Vehicle)? 2 Answers
Why is setting the parent of a text transform alters its world coordinates? 0 Answers