Search Unity

Change objects Y position during scene change

Discussion in 'AR/VR (XR) Discussion' started by cguengerich, Sep 9, 2019.

  1. cguengerich

    cguengerich

    Joined:
    Sep 5, 2018
    Posts:
    2
    I am needing help with changing an objects Y position during a scene change. I have a Pole that the player can interact with when playing. This pole is set to DoNotDestroy during a scene change. The issue I am running into is if the player moves this Pole to a location where the floor is lower than the new scene the Pole falls because there is nothing for it to collide with. Is there a way to store the X and Z position of the Pole and adjust the Y position to be the current Y position plus some amount so when the new scene is loaded the pole with Persist and gravity can take place to collide with the new floor?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,131
    Sure. Just write a script along the lines of:

    Code (csharp):
    1. pos = pole.transform.position;
    2. pos.y = newY;
    3. pole.transform.position = pos;
     
  3. cguengerich

    cguengerich

    Joined:
    Sep 5, 2018
    Posts:
    2
    Thank you for the help! I apologize for my ignorance but would I out this code in the script for my scene change? Also, how would I define my pos variable?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,131
    Yes, put this in whatever code is causing the scene change.

    As for pos, I should have shown that declared as a local variable right where it's used:

    Code (csharp):
    1. Vector3 pos = pole.transform.position;
    2. pos.y = newY;
    3. pole.transform.position = pos;
     
    cguengerich likes this.