Search Unity

transform.position not always getting called

Discussion in 'Scripting' started by Ragee, Jun 17, 2020.

  1. Ragee

    Ragee

    Joined:
    Nov 24, 2012
    Posts:
    15
    Hi,

    I was setting up a very simple platformer script that would set the player to go back to the start position after falling to a certain point.

    I noticed however that "transform.position = startPos" is not always getting called when entering the if statement.

    It seems random how many times it takes. Sometimes it is called on first time going through the if statement while other times it enters the if statement three times before the new transform position is set.

    This is the if statement
    Code (CSharp):
    1. if (transform.position.y < -20f)
    2. {
    3.             Debug.Log("Player Died");
    4.             transform.position = new Vector3(0f, 5f, 0f);
    5.      
    6. }
     
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    what is your problem?
     
  3. Ragee

    Ragee

    Joined:
    Nov 24, 2012
    Posts:
    15
    the problem is that the line - transform.position = new Vector3(0f, 5f, 0f); is not always getting executed

    Meaning that since the player is still below -20 on y it loops through again.
     
    Last edited: Jun 17, 2020
  4. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    you have that on update?
     
  5. Ragee

    Ragee

    Joined:
    Nov 24, 2012
    Posts:
    15
    Yes, have it under update. Tried it also as a public function and triggered by collison. The function runs but not the change in position. As a matter of fact I cannot get the transform position to change at all now.

    Found out now that if I disable the Character Controller it works. So there must be something I have done wrong there.
     
    Last edited: Jun 17, 2020
  6. Ragee

    Ragee

    Joined:
    Nov 24, 2012
    Posts:
    15
    Last edited: Jun 17, 2020