- Home /
My player still keep moving when i unparent it form the car
Hey guys, I am trying to get the player to hop into the car and drive the it and press 'e' to get out. I thought I might use transform.parent to move the car and the player at the same time and un-parent the player from the car when he gets out. Everything works except when I un-parent the player, it's decide to still move instead of coming to a dead stop. I have no clue what to do. Here is a video of what I mean YOUTUBELINK
public GameObject player;
public GameObject buggy;
public Camera playerCamera;
public Camera buggyCamera;
public BuggyMovementController buggyMovementController;
// Use this for initialization
void Start () {
buggyMovementController.enabled = false;
ChangeToPlayerCamera();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
ExitBuggy();
}
}
public void EnterBuggy()
{
player.SetActive(false);
ChangeToBuggyCamera();
buggyMovementController.enabled = true;
player.transform.parent = transform;
}
public void ExitBuggy()
{
player.SetActive(true);
ChangeToPlayerCamera();
buggyMovementController.enabled = false;
player.transform.parent = null;
}
public void ChangeToBuggyCamera()
{
buggyCamera.enabled = true;
playerCamera.enabled = false;
}
public void ChangeToPlayerCamera()
{
buggyCamera.enabled = false;
playerCamera.enabled = true;
}
Answer by StarlingSoftworksInteractive · Oct 02, 2017 at 09:09 PM
Thanks arnatus, I found a solution by looking at the docs. instead of parenting the player and the buggy, I manually set the player position when he exit the buggy
player.transform.position = exitPosition.position;
Answer by arnatus · Oct 02, 2017 at 04:08 AM
buggyMovementController.enabled = false; won't prevent it from being called so I suggest you try destroying the buggyMovementController instead.
Destroy(buggyMovementController);
Then add it back again via code after when the player enters the car. Try this solution and let me know otherwise I may have to get a closer look at your code.
Hey Thanks for answering, the player still moves straight ahead once i exit the car (without me pressing the keys). I am still scratching my head
I see what's going there. I think I misunderstood your question and your video made me think that the Player and the Car were still moving forward after unparenting them. Anyway glad you were able to find a way to fix it.
I also use character controller on my player