I am beginning work on a FPS that allows the player to enter/exit vehicles. I have a simple plane in the works. When the player touches a trigger collider attached to the plane, I want to attach the player to the plane. I am using a Character Controller for the player. The player doesn't have a rigidbody. The plane is a rigidbody that I control by adding forces.
I am setting the player tranform's parent to that of the plane; however the plane takes off and the player doesn't move. The player's transform magnitude rapidly increases from zero as the plane flies away and the player gameobject is in the plane's transform hierarchy, so the 'attach' is working. Once the player touches the plane I don't adjust its position at all.
//instant take off for now
plane.transform.position = new Vector3(1000, 100, 700);
plane.transform.rotation = Quaternion.AngleAxis(320, Vector3.up);
plane.rigidbody.velocity = plane.transform.forward * 50;
//attach player to plane and put them in the cockpit position
transform.parent = plane.transform;
transform.localPosition = plane.OwnerPosition; //currently 0,0,0
It seems that the player's localtransform is relative to the plane, but the player isn't attached as such.
Do I actually need to reset the player's transform.localPosition every update? I tried this and I found that when the plane was going really fast, the player would lag behind a bit.
Thanks for your help.
Edit: Updating the player's position to the plane's cockpit position (0,0,0 at the moment) in Update rather than FixedUpdate seems to help with the player lagging behind, though I need to test that further. I'm still not sure if this is the best way to go though.