Does transform.position work on a charactercontroller?

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

2 Likes

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.

Vector3 warpPosition = Vector3.zero;
public void WarpToPosition(Vector3 newPosition)
{
     warpPosition = newPosition;
}

void LateUpdate()
{
      if (warpPosition != Vector3.zero)
      {
             transform.position = warpPosition;
             warpPosition = Vector3.zero;
      }
}
1 Like

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!

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.

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

transform.position = startPos.position;

(Where startpos is a saved var that is set in the Awake function)

What do you suggest?[/code]

PS: Great tuts, I’m one of your customers! :wink:

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

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.

148 Likes

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.

14 Likes

Yes, this behaviour is changed in Unity 2018.3.0f2.

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.

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 ? :slight_smile:

1 Like

This works for me

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

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

4 Likes

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.

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.

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().enabled = false;
thePlayer.GetComponent().transform.position = mountainTop.gameObject.transform.position;
thePlayer.GetComponent().enabled = true;

2 Likes

This problem also can be fixed just by enabling Auto Sync Transforms in Physics settings

25 Likes

Ohhh it is really the best solution up to this case
thnk bro)

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

Worked a charm!

2 Likes