Search Unity

[Unity 2017 2.0f3] Player Object gets stuck on second respawn point.

Discussion in '2D' started by Arjay01, Dec 7, 2017.

  1. Arjay01

    Arjay01

    Joined:
    Aug 26, 2017
    Posts:
    9
    am not sure if its a unity bug or a wrong coding error. But I created two duplicated respawn points where the player "green box" spawns in the new position. The first spawn location worked perfectly but the second spawn location causes the player to spawn in that location and get stucked or slowly staggering all the way down the screen which didn't allow me to move.. even if the spawn location was in mid-air. Anyone know the fix?

    Checkpoint script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Checkpoint : MonoBehaviour {
    6.  
    7.     PlayerFunction pf;
    8.     PlayerMovement pm;
    9.     public bool playerSaved;
    10.     public GameObject checkspwnLoc;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         playerSaved = false;
    15.         pf = GameObject.Find("Player").GetComponent<PlayerFunction>();
    16.         pm = GameObject.Find("Player").GetComponent<PlayerMovement>();
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         if (playerSaved == true)
    23.         {
    24.             if (pf.playerDied == true)
    25.             {
    26.                 pm.rb.position = checkspwnLoc.transform.position;
    27.             } else
    28.             {
    29.                 pf.playerDied = false;
    30.             }
    31.         } else
    32.         {
    33.             playerSaved = false;
    34.         }
    35.     }
    36.  
    37.     void OnTriggerEnter2D(Collider2D collision)
    38.     {
    39.         playerSaved = true;
    40.     }
    41. }
    Playerfunction script (where the bool playerDied is)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerFunction : MonoBehaviour {
    7.  
    8.     // Limited lives
    9.     [HideInInspector]
    10.     public int lives;
    11.  
    12.     // Death Counter
    13.     public Text livesText;
    14.     public bool playerDied;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         lives = 0;
    19.         playerDied = false;
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         // Setting the death count to be accurate
    25.         livesText.text = "Deaths: " + lives.ToString();
    26.     }
    27. }
    28.  
     

    Attached Files: