- Home /
Attaching player transform to vehicle transform not moving player position
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.
Answer by Paulius-Liekis · Sep 27, 2010 at 09:31 AM
Sounds, weird attaching should just work (you're doing something like this: player.transform.parent = plane.transform), right? Maybe there is some script which is affecting players position (like some physics/character controller)?
Answer by Wolfram · Sep 27, 2010 at 10:43 AM
In general, a
player.transform.parent = plane.transform;
should suffice. You don't need (and don't want to!) reset player.transform.localPosition after that, because this value is automatically adjusted when you do the reparenting, so that "player" does not change its global position in world coords (The above line of code does exactly the same thing as dragging the player onto the plane in the hierarchy tab).
The problem you're experiencing sounds more like a collision/physics problem. I'm not sure how physics objects re supposed to behave if you attach them to one another, and apply a force to the other.
My first guess would be, the physics engine is very picky with large differences in positions between frames (e.g., trying to hit a tennis ball with a racket), and will not recognize the collision if the velocity is too large. Try to see if your problem persists if you use very low velocities for your plane. If that fixes your problem, use this script (forum discussion).
Your answer
Follow this Question
Related Questions
Does moving a charactercontroller via it's transform hurt performance? 1 Answer
How do i move CharacterController toward the direction its facing? 0 Answers
How to rotate an object without transform.Rotate 1 Answer
CharacterController.Move Not Corresponding to gameobject.transform.rotation 1 Answer
Transformers 1 Answer