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

Resolved On Start and On Enable not setting an objects position when it used to before with no issue

Discussion in 'Visual Scripting' started by Terxel, Jun 23, 2022.

  1. Terxel

    Terxel

    Joined:
    Aug 22, 2021
    Posts:
    6
    I have a main player script, there is an On Start node that will trigger a Transform Set Position, setting the players position to the Player Position application variable (Vec3).

    This worked for a while, up until just now. Nothing in the player script was changed, nothing that affects the Player Position application variable is changed, and when I inspect the Player Position variable it is being set correctly. The player is being set to the scenes default object position.

    I have switched the On Start to an On Enable several times back and forth and nothing seems to work. Using an Update function to check whether or not the variable is in fact being set right shows me that it is, as the player is being set in the correct position.

    I'm wondering if maybe there is something that automatically overwrites the objects position to the scenes default object position and its being triggered after the On Start and On Enable functions. I didn't have an issue with this until today.

    I've googled this issue and tried just about everything I could find to try and solve this. Any help is greatly appreciated.
     
  2. munchmo

    munchmo

    Joined:
    May 20, 2019
    Posts:
    82
    Have you tried closing and re-opening Unity? I've seen some strange behavior from the editor that is solved by restarting it.
     
  3. Terxel

    Terxel

    Joined:
    Aug 22, 2021
    Posts:
    6
    Yes, I've tried several times and even waited overnight to see if it was just one of those days. Unfortunately it seems this issue is deeper than that.

    Prior to the issue I was working on a door script and I was starting to test it out. When I entered one room I got curious and tried to enter the prior room and that's when the issue started to come up.
     
  4. munchmo

    munchmo

    Joined:
    May 20, 2019
    Posts:
    82
    That at least sounds like a starting point, what exactly does this door script do? Does it affect player position? Maybe revert that change or remove the door for testing. No idea what or why it would maintain some effect after stopping and reloading though.
     
  5. Terxel

    Terxel

    Joined:
    Aug 22, 2021
    Posts:
    6
    The door function is what changes the Player Position variable.

    When you interact with the door, theres a transition effect and the object referenced in the doors Transition Target variable has its position saved into the Player Position variable.

    I've gone through several times and can confirm that this part is working (looking at the application variable and looking at the referenced objects position, both match perfectly). The only part not working is the player, on start/on enable, is not setting its position according to the variable.

    The only change to the door script is replacing the raycasting part with a macro that does it (nothing in that macro references the players position, and it only exists for future objects that need raycasting so I dont have to redo the script).
     
  6. munchmo

    munchmo

    Joined:
    May 20, 2019
    Posts:
    82
    Ahhh, okay, that makes more sense. You say the variable is being set correctly, but then the player is being moved to another position altogether. Is the Player Position variable attached to the player object or something else? Can you post pictures of the graph?
     
  7. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please share your code, with Code Tags (from the edit bar ribbon)
     
  8. Terxel

    Terxel

    Joined:
    Aug 22, 2021
    Posts:
    6
    This is the part that should set the players position.

    upload_2022-6-23_10-54-37.png


    This is the whole door script upload_2022-6-23_10-57-8.png upload_2022-6-23_10-57-30.png

    and the player position variable is an application variable. All of this used to work prior, and I have an old build from months ago that doesn't have this issue.
     
  9. munchmo

    munchmo

    Joined:
    May 20, 2019
    Posts:
    82
    Wow, yeah, that's pretty straightforward, there's not a whole lot of room for something to break, lol. Although, I'm not seeing where you're setting the PlayerPosition variable, is that in the flow after the animator plays or part of the actual animation? Are you loading a new scene anywhere in this process?

    Some things that you probably already know but I feel need to be said for others' benefit. On Start will only ever trigger once, on the frame when a script is enabled just before any of the Update methods are called the first time regardless of whether or not the script is enabled. On Enable can run any number of times, but only whenever an object becomes enabled and active.

    Obviously, you probably already know the above, since you're been testing with both On Start and On Enable. One thing you can also test with is maybe adding a UI button really quick that'll call a Custom Event that flows into your Set Position for the Player. That way you can just push a button to see what happens anytime, instead of checking the whole thing at once. You could always add buttons that mimic the different pieces of the process.
     
  10. Terxel

    Terxel

    Joined:
    Aug 22, 2021
    Posts:
    6
    Just tried that with an On Keyboard Input and found something interesting.

    When I press space, the player is set to the variables position, but very quickly snaps back to its previous position.

    I have a Character Controller attached to the player, maybe that has something to do with it? Prior to about a month ago I was using rigid body on the player, but switched to Character Controller when I started implementing stairs, and the old build I tested was compiled before I had switched to the Character Controller.
     
  11. munchmo

    munchmo

    Joined:
    May 20, 2019
    Posts:
    82
    Wow, okay, sounds like it was working all along except not. Must be something in the way the character controller works that sets position every update or something.
     
  12. Terxel

    Terxel

    Joined:
    Aug 22, 2021
    Posts:
    6
    Figuring out that it was the character controller causing issues, I was able to google about the issue involving the character controller jumping back to a previous position and was able to find a solution


    although it doesn't feel efficient, it works
    upload_2022-6-23_12-11-41.png

    Thank you for your help