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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Trying to make my player re spawn on trigger

Discussion in 'Scripting' started by thesavvycow, Aug 24, 2018.

  1. thesavvycow

    thesavvycow

    Joined:
    Apr 3, 2018
    Posts:
    1
    Im trying to make a object re spawn when it falls out of the cameras view. I have set the object im using as a trigger & ive set my players tag to player. Idk what the problem is. Im bad at coding so pls keep the talk simple. Thanks in advance. hears my code

    [SerializeField] private Transform player;
    [SerializeField] private Transform respawnPoint;
    void OnTriggerEnter2D(Collider2D other)
    {
    player.transform.position = respawnPoint.transform.position;
    }
     
  2. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    365
    Is your object colliding with anything when it falls out of the camera view? Otherwise that function is never called. An alternative would be an Update function that checks the position, though I'm not sure which one is better for performance if you need a collider on the object anyway.
     
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Have you looked at OnBecameInvisible? ( no pun intended :D )