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

how to return the object to its original position

Discussion in 'Scripting' started by Trild123787898, Feb 28, 2020.

  1. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    I have an object which sometimes can move away from my original position, how can I first check that he has moved away and then return him to my position, I just can’t understand
     
  2. MichaelTheAvali

    MichaelTheAvali

    Joined:
    May 10, 2017
    Posts:
    37
    Store the original position in a variable then if it moves, move it back using the variable, Try using a transform variable for this.
    Sadly I'm using a computer that can't run visual studio right now so I can't really provide an example
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Trild123787898

    "I have an object which sometimes can move away from my original position, how can I first check that he has moved away and then return him to my position, I just can’t understand"

    You can do it just like @MichaelTheAvali said. Why not first write down the steps you need, and then try to do it.

    You can store the start position like this:
    Code (CSharp):
    1. // in your class
    2. Vector3 startPosition;
    3.  
    4. // in Start
    5. startPosition = yourObjectTra.position;
    Then when you want, check if the distance to startPosition (one way to do this) is greater than zero, move the object back to its start position.
     
  4. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    thanks I will punch