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. Dismiss Notice

Setting an object's position to another objects' without parenting or following

Discussion in 'Scripting' started by SuspecM, Sep 5, 2022.

  1. SuspecM

    SuspecM

    Joined:
    Jul 27, 2021
    Posts:
    23
    Simply setting transform.position to the other transform's position makes it always the same position, no matter how many variations I tried. I tried storing the position of the second object in a separate Vector 3, in separate floats corresponding to the coordinates and a few others. How can I just extract the second object's position at the moment and place the first transform onto that position?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    I personally find your post slightly confusing. You want ObjectA to be at ObjectB's position but
    ObjectA.transform.position = ObjectB.transform.position;
    isn't what you want?
     
  3. SuspecM

    SuspecM

    Joined:
    Jul 27, 2021
    Posts:
    23
    Sorry, should have clarified it a bit more. Whenever I try a simple ObjectA.transform.position = ObjectB.transform.position, ObjectA's position starts to follow ObjectB's no matter what. I'd like to make it so ObjectA gets moved to ObjectB's position, but after that I can freely move ObjectA without moving ObjectB.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Can you post your code?

    Without parenting the two objects, then assigning the position to another position should only occur once. Now, if you run it in every frame in Update, it'll execute every single frame and feel like it has been parented.
     
  5. SuspecM

    SuspecM

    Joined:
    Jul 27, 2021
    Posts:
    23
    Oh yeah I was dumb and was constantly calling it in the update. That explains a lot. Thanks for the help!