0
votes

I'm making a unity 3D game, part of the game allows the player to get into a car and drive it.

Inside the car I have put a "seat" GameObject whose position and rotation is used to determine where the player will sit.

When I want the player to sit in the car, I make the car the players parent, then set the player transform position and rotation to that of the seat.

Here is the C# code

 // make player's parent the same as the seats parent (which is the car)
 transform.parent = seat.transform.parent;

 // put player in the exact position and rotation as the seat
 animator.transform.position = transform.position = seat.transform.position;
 animator.transform.rotation = transform.rotation = seat.transform.rotation;

 animator.transform.localPosition = transform.localPosition = seat.transform.localPosition;
 animator.transform.localRotation = transform.localRotation = seat.transform.localRotation;

This seems like it should work, but what ends up happenning is that for some reason the player does not end up perfectly in the seat, but some short distance away from it, and also the players rotation doesnt match the seat, instead ends up slightly off. So the player looks like he is not really in the seat but floating near it, and turned around in some other direction.

Anybody knows what I'm doing wrong?

2

2 Answers

0
votes

position specifies an object's location in the world. If you take two objects and assign the same position, then they will both be in the same place.

localPosition specifies an object's location relative to its parent. If you take two objects and assign the same localPosition, the outcome will depend on the position of each object's parent. If they have different parents, different scaling, or different rotation, they may end up in different places.

Roughly the same idea applies to rotation and localRotation.

You seem to have confused the relationship between world-space and local-space coordinates. It's very important to understand the difference, and how they relate to the scene hierarchy.

If you want to put both objects in the same place, this should suffice:

transform.position = seat.transform.position;
transform.rotation = seat.transform.rotation;

If you want the object to move with the car, afterward, you could also reassign its parent:

transform.parent = seat.transform.parent;
0
votes

Check your player object's pivot point.It might not be on position you expect.If it is not, try to make it point where you want by dragging your model.