Search Unity

Objects Flying Away when Unparented

Discussion in 'Physics' started by JackMySon, Jul 16, 2018.

  1. JackMySon

    JackMySon

    Joined:
    Apr 15, 2018
    Posts:
    13
    I'm fairly new to Unity as well as coding, but I think I have a decent grip on what I'm trying to do... at least I think. I have set up scripting so that a player can enter and exit a vehicle from the standard assets. When exiting the vehicle, the player gets unparented from the vehicle and gets moved away from the vehicle to try and avoid collisions (I know that the player could get put in a wall though).

    The issue is that when the exit vehicle function is called, the player gets moved and unparented, however, the vehicle goes flying away as thought something launched it into the air.

    Below is the function I use to exit the vehicle. Keep in mind that this script is attached to the player character. Thanks for any responses in advance!

    Code (CSharp):
    1. public void ExitVehicle(GameObject vehicle)
    2.     {
    3.         VehicleConfigScript v = vehicle.GetComponent<VehicleConfigScript>();
    4.  
    5.         GetComponent<CharacterController>().enabled = true;
    6.         GetComponent<FirstPersonController>().enabled = true;
    7.         cam.GetComponent<NoclipScript>().enabled = true;
    8.  
    9.         carry.SetActive(true);
    10.  
    11.         vehicle.GetComponent<CarUserControl>().enabled = false;
    12.         vehicle.GetComponent<CarController>().enabled = false;
    13.  
    14.         transform.position += new Vector3(3, 2, 0);
    15.  
    16.         transform.SetParent(null, true);
    17.  
    18.         v.cam.SetActive(false);
    19.  
    20.         cam.SetActive(true);
    21.  
    22.         inVehicle = false;
    23.     }