0
\$\begingroup\$

I am relatively new to Unity, and I am currently working on some code. I am trying to change the position of Unity's standard assets first-person controller using

player.position = anotherObject.position + offset;

I'd like to add that I am setting the player's position when it collides with a box collider.

However, this isn't working for some reason. I've tried debugging it using print statements before and after the teleportation of the player, and this is what I see:

Player position: (0, 2, 0)
Player position after teleport: (32, 2, 0)
Player position one frame later: (0, 2, 0)

After one frame, the player returns to it's original position, as if it was never moved.

Here is my code, if it will help you better understand my problem.

public class PortalTeleporter : MonoBehaviour {

    public Transform player; // The player
    public Transform receiver; // Receiving portal

    private bool playerOverlap = false; // boolean to store whether player touches the box collider on portal

    void Update(){
        if(playerOverlap){
            Vector3 portalToPlayer = player.position - transform.position;
            float dotProduct = Vector3.Dot(transform.up, portalToPlayer);
            if(dotProduct < 0f){
                float rotationDifference = Quaternion.Angle(transform.rotation, receiver.rotation);
                rotationDifference += 180;
                player.Rotate(Vector3.up, rotationDifference);

                Vector3 positionOffset = Quaternion.Euler(0f, rotationDifference, 0f) * portalToPlayer; // Offset between player and portal
                Vector3 pos = receiver.position + positionOffset;
                print(player.position);
                player.position = pos;
                print(player.position);
            }
        }
    }

    // Triggered when player enters the portal
    void OnTriggerEnter(Collider other){
        if(other.tag == "Player"){
            playerOverlap = true;
        }
    }

    // Triggers when player untouches the portal
    void OnTriggerExit(Collider other) {
        if(other.tag == "Player"){
            playerOverlap = false;
        }
    }

Thank you for taking the time to answer my question!

\$\endgroup\$
  • \$\begingroup\$ Did you check the first person controller script to see if it's caching and re-asserting a position? \$\endgroup\$ – DMGregory Nov 24 at 0:29
  • \$\begingroup\$ @DMGregory I just looked throught the fp controller script and it doesn't seem to be caching and reasserting positions \$\endgroup\$ – Bill Bai Nov 24 at 1:29
1
\$\begingroup\$

So if any of you currently land on this thread in the future and is wondering why your FPController isn't teleporting, I solved this issue by updating my FPController to the latest one on the unity asset store (Not the 2017 one). Hope this helps any of you who had the same problem as I did!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.