Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

"Transform.parent" won't transform.

Discussion in 'Scripting' started by dasschlumpf, Apr 30, 2015.

  1. dasschlumpf

    dasschlumpf

    Joined:
    Apr 29, 2015
    Posts:
    7
    Hello folks!

    Apparently when i try to transform my Character with an moving Object (vehicle) it's not transforming at all.
    So the following script is being called once a Character tries to enter a Vehicle.

    Code (JavaScript):
    1. else if (playerInVehicle == false && whichcar != null)
    2.                 {
    3.                     //gameObject.GetComponent(Shooting).enabled = false;
    4.                     cameramain.transform.parent = whichcar.transform;
    5.                     Char.GetComponent("PlayerMovement").enabled = false;
    6.                     Char.GetComponent("MouseLook").enabled = false;
    7.                     playerInVehicle = true;
    8.                     cameramain.GetComponent(CarCamera).enabled = true;
    9.                     cameramain.GetComponent(CarCamera).target = whichcar.GetComponent(CarController).centerOfMass.transform;
    10.                     Char.transform.parent = whichcar.transform;
    11.                     whichcar.GetComponent(CarController).iminside = true;
    12.                     //Adam.SetActive(false);
    13.                     //whichcar.GetComponent(SoundController).enabled = true;
    14.                  
    15.              
    16.              
    17.                 }
    Char is set to the current Character Clone.
    whichcar is set to the nearest object he tries to enter.

    EDIT: I just realized that the character is actually moving with the car, just really really slowly.The Character moves 1 inch in the direction the car is driving when the car moved already 50 meters.
    Any ideas?
     
    Last edited: Apr 30, 2015
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Is it possible that it just looks really odd because you're not setting the player's local position after parenting? Try:
    Code (csharp):
    1.  
    2. Char.transform.parent = whichcar.transform;
    3. Char.transform.localPosition = Vector3.zero;
    4.  
    And see if that works better?
     
  3. dasschlumpf

    dasschlumpf

    Joined:
    Apr 29, 2015
    Posts:
    7
    The players position after parenting is where the player entered the Car.
    I tried it with:
    Code (JavaScript):
    1. Char.transform.localPosition = Vector3(0,5,0);
    This transforms the char on top of the car.


    Vector3.zero made him transform under the car.

    Apparently its the same issue again.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Do you have a collider or rigidbody you have to disable as well? I can't seem to mimic your issue of the player just getting stuck or moving incredibly slowly.



    And here's how I parent, from inside the vehicle script with a linked Player transform:

    Code (csharp):
    1.  
    2.     if(Input.GetKeyDown(KeyCode.Space) == true)
    3.      {
    4.        if (player.parent == null)
    5.        {
    6.          player.parent = transform;
    7.          player.localPosition = Vector3.zero;
    8.        }
    9.        else
    10.        {
    11.          player.parent = null;
    12.          player.position = transform.position;
    13.        }
    14.      }
    15.  
     
  5. dasschlumpf

    dasschlumpf

    Joined:
    Apr 29, 2015
    Posts:
    7
    Disabling Rigidbody and changing the code order did the trick! Thank you so much :)