Search Unity

Level 2 Fall/Respawn

Discussion in '2D' started by BookwormGames, Nov 29, 2018.

  1. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    Hello! For my 2D Platformer, death and respawn works just fine in Level 1. However, it doesn't work for Level 2. Here are my Player Controller and Level Manager codes. What should I change in order to have death/respawn work in Level 2 as it does in 1? Thanks for your help!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float speed = 5f;
    8.     public float jumpSpeed = 8f;
    9.     private float movement = 0f;
    10.     private Rigidbody2D rigidBody;
    11.     public Transform groundCheckPoint;
    12.     public float groundCheckRadius;
    13.     public LayerMask groundLayer;
    14.     private bool isTouchingGround;
    15.     private Animator playerAnimation;
    16.     public Vector3 respawnPoint;
    17.     public LevelManager gameLevelManager;
    18.  
    19.     Vector2 tmpTransform;
    20.  
    21.     // Use this for initialization
    22.     void Start()
    23.     {
    24.         rigidBody = GetComponent<Rigidbody2D>();
    25.         playerAnimation = GetComponent<Animator>();
    26.         respawnPoint = transform.position;
    27.         gameLevelManager = FindObjectOfType<LevelManager>();
    28.  
    29.         tmpTransform = transform.localScale;
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.         isTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer);
    36.         movement = Input.GetAxis("Horizontal");
    37.         if (movement > 0f)
    38.         {
    39.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    40.             transform.localScale = tmpTransform;
    41.  
    42.         }
    43.         else if (movement < 0f)
    44.         {
    45.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    46.             transform.localScale = new Vector2(-tmpTransform.x, tmpTransform.y);
    47.         }
    48.         else
    49.         {
    50.             rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
    51.         }
    52.         if (Input.GetButtonDown("Jump") && isTouchingGround)
    53.         {
    54.             rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpSpeed);
    55.         }
    56.         playerAnimation.SetFloat("Speed", Mathf.Abs(rigidBody.velocity.x));
    57.         playerAnimation.SetBool("OnGround", isTouchingGround);
    58.     }
    59.  
    60.     private void OnTriggerEnter2D(Collider2D other)
    61.     {
    62.         if (other.tag == "FallDetector")
    63.         {
    64.             gameLevelManager.Respawn();
    65.         }
    66.         if (other.tag == "CheckPoint")
    67.         {
    68.             respawnPoint = other.transform.position;
    69.         }
    70.     }
    71.  
    72.  
    73. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class LevelManager : MonoBehaviour {
    7.     public float respawnDelay;
    8.     public PlayerController gamePlayer;
    9.     public int coins;
    10.     public Text coinText;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         gamePlayer = FindObjectOfType<PlayerController>();
    15.         coinText.text = "Coins: " + coins;
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.        
    21.     }
    22.  
    23.     public void Respawn () {
    24.         StartCoroutine ("RespawnCoroutine");
    25.     }
    26.  
    27.     public IEnumerator RespawnCoroutine() {
    28.         gamePlayer.gameObject.SetActive(false);
    29.         yield return new WaitForSeconds (respawnDelay);
    30.         gamePlayer.transform.position = gamePlayer.respawnPoint;
    31.         gamePlayer.gameObject.SetActive(true);
    32.     }
    33.  
    34.     public void AddCoins(int numberOfCoins) {
    35.         coins += numberOfCoins;
    36.         coinText.text = "Coins: " + coins;
    37.     }
    38. }