Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

transform.position set only for one frame

Discussion in 'Scripting' started by DavidSandrik, Jun 11, 2019.

  1. DavidSandrik

    DavidSandrik

    Joined:
    Aug 15, 2018
    Posts:
    18
    Hi,

    I would like to set a position of a gameObject within attached OnTriggerEnter function, nested in IF statement. After some debugging I saw that desired position was set to an object only for one frame and in next frame position of the object went back where it was before. Am I missing something, is this normal behavior?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Objects do not change positions by themselves. It must be changed by your or system script. Aside from another your script changing the position, it might be phisycs, animation or something else.
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    No, that is neither normal nor expected behaviour. You will find that you are setting the position of the object in some other place - maybe even another script. Objects don't Change their transform by themselves.
     
  4. DavidSandrik

    DavidSandrik

    Joined:
    Aug 15, 2018
    Posts:
    18
    thanks for suggestions. I am working with FPS player from Standard assets, maybe there is something.
    I am trying to mage a "re-spawn" effect, when player drops off the terrain he triggers box collider and it moves him back to terrain. I will take in consideration what you suggested, but for now I get re-spawn only for one frame after that player moves to position where he hit the box collider.
     
  5. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    Its probably controlled by a rigidbody, and your setting the transform, which means once fixedupdate kicks in it moves back to where the rigidbody is.

    Try setting the rigidbody position instead
     
  6. DavidSandrik

    DavidSandrik

    Joined:
    Aug 15, 2018
    Posts:
    18
    Thanks. I will try that.
     
  7. DavidSandrik

    DavidSandrik

    Joined:
    Aug 15, 2018
    Posts:
    18
    Just to let you know, mistake was made by rigidbody interpolation. On default position was taken from previous frame. By setting it to extrapolate player does not move to old position after setting new vector3.