#Change player position with other script

1 messages · Page 1 of 1 (latest)

livid siren
#

So i'm trying to set player position with GoOutsideScript. In my scene i have 2 game object to and it's called ExitPoint, and EnterPoint.

The script goes like this :

public Animator FadeAnimation;
public GameObject player;
public Transform ExitPoint;
public Transform EnterPoint;
PlayerStateMachine playerAttribute;
float distance;

private void Update()
    {
        if (player)
        {
            distance = Vector3.Distance(transform.position, player.transform.position);
            playerAttribute = player.GetComponent<PlayerStateMachine>();

            if (distance < 15)
            {
                if (playerAttribute.OutsideHouse == false)
                {
                    if (Input.GetKeyDown(KeyCode.E))
                    {
                        //Start couroutine to go outside
                        StartCoroutine(goOutside());
                    }
                }
                else if (playerAttribute.OutsideHouse == true)
                {
                    if (Input.GetKeyDown(KeyCode.E))
                    {
                        //Start couroutine to go inside
                        StartCoroutine(goInside());
                    }
                }
            }
        }
    }

    IEnumerator goOutside()
    {
        yield return new WaitForSeconds(1f);
        player.transform.position = new Vector3(ExitPoint.position.x, ExitPoint.position.y, ExitPoint.position.z);
    }

    IEnumerator goInside()
    {
        yield return new WaitForSeconds(1f);
        player.transform.position = new Vector3(EnterPoint.position.x, EnterPoint.position.y, EnterPoint.position.z);
    }

When i press E, the player position didn't change to the ExitPoint, it just stuck inside the house

dreamy sunBOT
fading flume
#

The CharacterController has its own internal value for tracking its position, which overrides the transform position

#

If you want to set the players position, disable the controller, set it, then re-enable it

livid siren
#

The player is using character controller

fading flume
livid siren
# fading flume If you want to set the players position, disable the controller, set it, then re...

This is working
I didn't know that the CharacterController component override the transform position

Just like you said i disable the controller first and then re-enable it

playerAttribute.PlayerControl.enabled = false;
        yield return new WaitForSeconds(0.5f);
        player.transform.position = new Vector3(ExitPoint.position.x, ExitPoint.position.y, ExitPoint.position.z);
        yield return new WaitForSeconds(0.5f);
        playerAttribute.PlayerControl.enabled = true;

And it works like exactly the way i wanted. Thank you so much for the help