teleport makes the player to stop moving

Hi Guys,

In this little game (i’m trying to learn the UFO project), i try to make my player teleport when he reaches count of 3. My player téléports, but i’m unable to make it move afterwards (lines 22 to 27 no longer works after the teleport). I think the error is in the lines 29 to 32, but i can’t find it :frowning:

thanks for any input

Here is the script:

using UnityEngine; 
using System.Collections;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour {

    public float vitessedujoueur;
    public Text countText;
    public Text winText;
    private Rigidbody2D rb2d;
    private int count;
    public Transform dest;

    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
        count = 0;
        winText.text = ""; 
        SetCountText();
    }

    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector2 mouvement = new Vector2(horizontal, vertical);
        rb2d.AddForce(mouvement * vitessedujoueur);

        if (count >= 3)
        {
            this.gameObject.transform.position = dest.position;
            Camera.main.transform.position = dest.position;

        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("PickUp")) 
        {
            other.gameObject.SetActive(false);
            count = count + 1;
            SetCountText();
        }
    }


    void SetCountText()
    {
        countText.text = "Count:" + count.ToString();
        if (count >= 10)
            winText.text = "BRAVO!";
    }
}

The problem is that after count is greater than or equal to 3, you continuously teleport them inside your FixedUpdate which is called (almost) every frame.

Better to teleport them when, and only when, they actually reach count 3,

So just move the if(count >= 3) code to inside the OnTriggerEnter2D code right below where you assign count = count + 1, and then it will only teleport them when the event actually happens.