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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Does transform.position work on a charactercontroller?

Discussion in 'Scripting' started by tornadotwins, Dec 4, 2009.

Thread Status:
Not open for further replies.
  1. tornadotwins

    tornadotwins

    Joined:
    May 21, 2008
    Posts:
    308
    Hey folks!
    I'm trying to warp my player to a certain position when it collides with a gameObject.
    The collision works fine, but when I try to set the players position, using
    transform.position = newPos.position;
    Nothing happens.
    The player stays at exactly the same position.
    This script is attached to the player gameObject.

    It's driving me crazy.

    Perhaps it's conflicting with the move script I have in the Update function (the usual controller.SimpleMove()), but I bypassed it and it still didn't do anything.
    No errors either.

    So I searched long and hard to see if there's a different way to set the position of my player, but everyone seems to have success using transform.position...

    Do you have a clue what's going on?

    Any help would be awesome!
    Thanks guys!

    -Tornado222
     
    hako-975 and chasedig like this.
  2. calveit

    calveit

    Joined:
    Jul 4, 2009
    Posts:
    42
    Your CharacterController probably overrides your transform changes (it behaves more or less like a Rigidbody). You can try to store this new position and execute transform.position assignment in LateUpdate function in your script.

    Code (csharp):
    1.  
    2.  
    3. Vector3 warpPosition = Vector3.zero;
    4. public void WarpToPosition(Vector3 newPosition)
    5. {
    6.      warpPosition = newPosition;
    7. }
    8.  
    9. void LateUpdate()
    10. {
    11.       if (warpPosition != Vector3.zero)
    12.       {
    13.              transform.position = warpPosition;
    14.              warpPosition = Vector3.zero;
    15.       }
    16. }
    17.  
     
    chasedig likes this.
  3. tornadotwins

    tornadotwins

    Joined:
    May 21, 2008
    Posts:
    308
    Thanks for the help dude. I tried it in the lateUpdate() function but it STILL won't budge. Player just keeps on going at the same location. Collision is true.
    I can't believe I can't figure this out!! Something THIS simple!
    ...
     
  4. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Put a Debug.Log(transform.position) after the position change. If it prints out that you're at your old position, it failed. If it prints out that you're in your new position, then you're moving something that doesn't contain all the pieces of your player.
     
  5. tornadotwins

    tornadotwins

    Joined:
    May 21, 2008
    Posts:
    308
    Thanks GargerathSunman.
    I did just that and it turns out the position doesn't change at all.
    It's exactly the same before and after

    Code (csharp):
    1. transform.position = startPos.position;
    (Where startpos is a saved var that is set in the Awake function)

    What do you suggest?[/code]
     
  6. tornadotwins

    tornadotwins

    Joined:
    May 21, 2008
    Posts:
    308
    PS: Great tuts, I'm one of your customers! ;-)
     
  7. tornadotwins

    tornadotwins

    Joined:
    May 21, 2008
    Posts:
    308
    Fixed it!
    Really don't know how... but it's fixed now.
    I manually set the transform.position to a 'Vector3(int,int,int); and that worked.

    Sorry about the hassle guys... ;-(

    -Tornado222
     
  8. HS_Dave

    HS_Dave

    Joined:
    Nov 23, 2013
    Posts:
    2
    I know this is very old, but it's one of the few results that google gave to me when searching for a similar problem. I can confirm that a character controller will reset your direct transform.postion changes back to the previous location. If you wish to directly set the transform for a teleport or whatever you can fix this with:

    charController.enabled = false;
    charController.transform.position = new Vector3(some location);
    charController.enabled = true;

    Hopefully that will help some poor soul lost on the internet again in the future.
     
    CoolBeanz7, Lekret, Szappy and 104 others like this.
  9. morphinegames

    morphinegames

    Joined:
    Mar 13, 2015
    Posts:
    33
    THANK YOU! I just ran into this today with Unity 2018.3.0f2, previously I never ever had this problem with the CharacterController. I actually thought I was going crazy. You sir are a life saver.
     
  10. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,816
    Yes, this behaviour is changed in Unity 2018.3.0f2.
     
  11. MonkeyZero

    MonkeyZero

    Joined:
    Feb 20, 2013
    Posts:
    37
    While this fixes the position issue, set rotation for me is still 100% broken. I haven't been able to get my character controller to use the new Rotation of my Target Game object to save my life.

    What I myself am trying to do.

    I have a game object that I can place into the scene that acts as a sort of Target to set the Rotation and Position from. With turning off the CharacterController, I can now easily move the controller, but I can't rotate it away from what it was before the transform adjustment was called. even with turning off the CharacterController while this happens.

    Maybe I am going about this wrong, but it would follow that if you can move the CharacterController in this way, you should also be able to rotate it like this as well. But apparently not.
     
  12. Stormrage74

    Stormrage74

    Joined:
    Jan 7, 2019
    Posts:
    3
    Hi, giving my two cents on this question, i ran into this problem this day. I Believe that all is about the characterController, but i did not find a response in time so first of all thanks you for the enable/disable trick.
    For the rotation, you can do the same thing by changing the maincamera rotation to keep the rotation you want. The disadvantage of this is that it broke the direction system, apparently based on the initial camera rotation.

    So time to make our own CharacterController am i wrong ? :)
     
    chasedig likes this.
  13. Happyday

    Happyday

    Joined:
    Oct 10, 2015
    Posts:
    3
    This works for me

    public void ChangePositionController(GameObject targetObj)
    {
    StartCoroutine(ChangePos(targetObj));
    }

    IEnumerator ChangePos(GameObject targetObj)
    {
    PlayerObject.GetComponent<FirstPersonController>().enabled = false;
    yield return null;
    PlayerObject.transform.position = targetObj.transform.position;
    yield return null;
    PlayerObject.GetComponent<FirstPersonController>().enabled = true;
    }
     
  14. MonkeyZero

    MonkeyZero

    Joined:
    Feb 20, 2013
    Posts:
    37
    I'm out and about today, so I am not in a position to to give this a try. But when I get back home, I'm going to be sure to give this a go.

    Thanks for sharing your code.
     
  15. katarn321

    katarn321

    Joined:
    May 8, 2019
    Posts:
    5
    Thanks for these posts. Been pulling my hair out on this. I think I have a debug statement about ever other line of the FixedUpdate method of the FirstPersonController script. Confirmed it was the characterController.Move() call that was resetting my players position.

    The disable, set & enable trick worked for me.

    Im on Unity 2018.3.11f1.
     
  16. Peppermage

    Peppermage

    Joined:
    Apr 27, 2019
    Posts:
    2
    You are a saint! Your kindness in posting this save my bacon.

    Passing it along, this is how it looked for me in 2019.1.1:

    public GameObject thePlayer; //PlayerOneController
    public GameObject mountainTop; //Empty GameObject at Mountain Top location
    ....
    thePlayer.GetComponent<CharacterController>().enabled = false;
    thePlayer.GetComponent<CharacterController>().transform.position = mountainTop.gameObject.transform.position;
    thePlayer.GetComponent<CharacterController>().enabled = true;
     
    sofotole and konsic like this.
  17. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    377
    This problem also can be fixed just by enabling Auto Sync Transforms in Physics settings
     
  18. Danteck

    Danteck

    Joined:
    Mar 3, 2018
    Posts:
    1
    Ohhh it is really the best solution up to this case
    thnk bro)
     
  19. babbel-ford

    babbel-ford

    Joined:
    Nov 24, 2017
    Posts:
    8

    This was amazingly helpful. Had been trying to turn off the alterations of CharacterController for days with no solution from my search until this post. Great stuff
     
  20. TheRealTeviel

    TheRealTeviel

    Joined:
    Dec 29, 2015
    Posts:
    14
    Worked a charm!
     
    chasedig likes this.
  21. Peckingjay

    Peckingjay

    Joined:
    May 25, 2019
    Posts:
    1
    Auto Sync Transforms is there for backwards compatibility, just use Physics.SyncTransforms(); to flush changes made through the Transform to the Physics System.
     
  22. Kzapas

    Kzapas

    Joined:
    Oct 28, 2018
    Posts:
    2
    Here's my solution:
    Code (CSharp):
    1. public CharacterController thePlayer;
    2.     public Transform warpTarget;
    3.  
    4.     private void OnTriggerEnter(Collider _col)
    5.     {
    6.         if (_col.gameObject.CompareTag("Player"))
    7.         {
    8.             CharacterController cc = thePlayer.GetComponent<CharacterController>();
    9.  
    10.             cc.enabled = false;
    11.             thePlayer.transform.position = warpTarget.transform.position;
    12.             cc.enabled = true;
    13.  
    14.         }
    15.        
    16.     }
     
    IGDPD likes this.
  23. MrGreenish

    MrGreenish

    Joined:
    Oct 20, 2019
    Posts:
    28
    Thanks a bunch! I struggled with this for hours.
     
  24. MrGreenish

    MrGreenish

    Joined:
    Oct 20, 2019
    Posts:
    28
    I'm quite sure your Character Controller does NOT handle the rotation. Find the part of your script that handles the rotation. You probably have a part of the script that creates a new Vector3 based on your mouse input. Find the end of your rotation method and look for something that looks something like
    Code (CSharp):
    1. transform.localRotation = Quaternion.Euler(/* the new Vector3 */);
    You need to change transform.localRotation to be the your new rotation. You probably rotate the player game object for your x value and the camera for your y value.
     
  25. Shinobi-Sparda

    Shinobi-Sparda

    Joined:
    Mar 25, 2020
    Posts:
    2
     
  26. Shinobi-Sparda

    Shinobi-Sparda

    Joined:
    Mar 25, 2020
    Posts:
    2
    Thanks man, you just saved my life
     
  27. edward77

    edward77

    Joined:
    Aug 14, 2018
    Posts:
    1
    Thank you, worked like a charm
     
  28. Ragee

    Ragee

    Joined:
    Nov 24, 2012
    Posts:
    15
    I am using a NavMeshAgent to control the player and by handling that the same was as the character controller managed to move the player.
     
    kbkev78 likes this.
  29. psyconius

    psyconius

    Joined:
    May 16, 2018
    Posts:
    5
    Checking in from August 2020, and the disabling the CharacterController, setting, and enabling again just saved me from a melt down. I think I understand WHY this is necessary, but still annoying. I can't remember why I decided to go with CC's rather than RB's for this project, but it's been.... fun.
     
  30. msibrava

    msibrava

    Joined:
    Mar 18, 2019
    Posts:
    5
    Yep!!!
    That's the one!

    GetComponent<CharacterController>().enabled = false;
    transform.position = spawnPoints.transform.position;
    GetComponent<CharacterController>().enabled = true;

    It was driving me nuts, just like all you all.
     
    Justin-Wasilenko likes this.
  31. NekoLarissa

    NekoLarissa

    Joined:
    Feb 1, 2015
    Posts:
    1
    Thank you so much searched for an hour for a good solution thanks
     
  32. xofreshxo

    xofreshxo

    Joined:
    Apr 12, 2017
    Posts:
    66
    I used this and it worked perfectly. Are there any drawbacks to setting this?
     
  33. theakdev

    theakdev

    Joined:
    Nov 28, 2017
    Posts:
    5
    Thank you Thank you Thank you
    was driving me crazy for two days u really made my day
     
  34. theakdev

    theakdev

    Joined:
    Nov 28, 2017
    Posts:
    5
    For Navmeshagent you can also use the warp function
     
  35. MZ_Dino

    MZ_Dino

    Joined:
    Dec 11, 2020
    Posts:
    1
    Thank you! you have saved me from the torment!
     
  36. justDax

    justDax

    Joined:
    Mar 29, 2020
    Posts:
    3
    I am working on a multiplayer game, using Mirror and couldn't syncronize the player position between the clients.
    Turns out that the Character Controller was the culprit that prevented the Network Transform script to do its job.

    Thanks to @nbg_yalta that in my opinion pointed to the best solution:
    Edit -> Project Settings -> Physics -> check Auto sync Transforms

    the disable and enable between transforms while it can work, it feels like a bad hack.
     
    Chrisdbhr likes this.
  37. ldgarrett67

    ldgarrett67

    Joined:
    Nov 20, 2020
    Posts:
    1
    So glad I came across your reply. I have been beating my head against the wall for hours. Yet, so simple. THANK YOU
     
  38. guptaharsh615

    guptaharsh615

    Joined:
    Jul 3, 2020
    Posts:
    2
    Nice solution ... It keeps character controller in sync
     
  39. Lancelot7

    Lancelot7

    Joined:
    Jul 6, 2019
    Posts:
    2
    Hace 2 años me encontré con este problema y tuve que leer todo el código de fpsController para entender cómo funciona. Aunque mi problema fue con la rotación. La solución fue ir a MouseLook y arreglarlo desde allí.;)
     
  40. morgade

    morgade

    Joined:
    Apr 26, 2016
    Posts:
    1
    This is the winner !
     
  41. sofotole

    sofotole

    Joined:
    Jan 27, 2021
    Posts:
    2
    Youre Amazing my guy been searching for an answer to this problem for a while,Thanks.
     
  42. BaraShiro

    BaraShiro

    Joined:
    Jul 10, 2017
    Posts:
    4
    This is the one.
     
  43. JakeX

    JakeX

    Joined:
    Sep 3, 2012
    Posts:
    2

    Cheers, helped me out.
    Currently working stuff out in VR and was trying to teleport the Player Prefab that came with Oculus Intergration but kept getting flung around.
    This solved it no problem.
     
  44. power-game-master

    power-game-master

    Joined:
    Mar 24, 2021
    Posts:
    1
    Thanks, this helped me a lot!
     
    Alexx019 likes this.
  45. bittuz

    bittuz

    Joined:
    Nov 12, 2013
    Posts:
    5
    wow, thank you mate, you saved my day!
     
  46. Alexx019

    Alexx019

    Joined:
    Apr 9, 2021
    Posts:
    1
    I know its a bit late, but thank you so much! I thought i was going insane, nothing worked and nobody else seemed to have experienced this issue!
     
  47. ThisIsAlex

    ThisIsAlex

    Joined:
    Jul 24, 2020
    Posts:
    1
    thank you. thank youthank youthank youthank youthank youthank youthank youthank youthank youthank youthank you
     
  48. lirith_unity

    lirith_unity

    Joined:
    Feb 21, 2020
    Posts:
    1
    @HS_Dave: Thank you Sir. You helped me a lot *smile*
     
  49. charlesz_

    charlesz_

    Joined:
    Oct 17, 2020
    Posts:
    1
    I am that poor lost soul thank you so much.
     
  50. kknich

    kknich

    Joined:
    Aug 17, 2021
    Posts:
    2
    god bless you, just god bless you
     
    Seg4- likes this.
Thread Status:
Not open for further replies.