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

Question CharacterController is snapping my character back after loading

Discussion in 'Scripting' started by Acce010, Jan 29, 2023.

  1. Acce010

    Acce010

    Joined:
    Aug 23, 2017
    Posts:
    4
    I hope I'm right here.
    First my setup. I'm using the ThirdPersonController from the StarterAsset-Pack, so my GameObject has a CharacterController and ThirdPersonController component.
    Know when I load my game the scene is getting new loaded. Before applying the saved data I wait one frame. If I go frame by frame I can see that my character has the correct saved position, but in the next FixedFrame when ThirdPersonController.Move() is getting called, my position is back to the "scene default" after CharacterController.Move() (see pictures). In SceneView I can also see the GameObject switching the position between frames. If I disable the ThirdPersonController complete my GameObject keeps the correct saved position, so the problem is there.
    I hope someone know's a solution, other then using another Controller.
     

    Attached Files:

  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Character Controllers keep track of their own positions, they don't use the transform's position. You didn't show any code, but I'm guessing you set the position of the transform to move it at the beginning, and then the Character Controller overrode that position by putting it back. Easiest solution is to just disable and re-enable the Character Controller when moving the object, as I'm pretty sure it picks up the new position when enabled.

    You can actually find this in the issue tracker from Unity, and the response suggested either enabling auto-sync transforms inside of the Unity Physics Settings, or to just sync it manually by calling Physics.SyncTransforms, but those seem a bit more expensive than the above solution.
     
    Acce010 and chemicalcrux like this.
  3. Acce010

    Acce010

    Joined:
    Aug 23, 2017
    Posts:
    4
    Your guess was right :)
    And your solution works perfectly. Thank you :)