Search Unity

Camera follows player after death.

Discussion in 'Scripting' started by nomoxd, Oct 4, 2019.

  1. nomoxd

    nomoxd

    Joined:
    Oct 3, 2019
    Posts:
    2
    Hi, is there any way anyone can help me with this situation?
    When the player falls into the "void" the player dies. However, the camera follows with him.
    here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour
    6. {
    7.     public GameObject player;
    8.     private Vector3 offset;
    9.     public bool gameOver;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         transform.position = player.transform.position + offset + new Vector3(5, 5, -15);
    21.  
    22.         if (gameOver == true)
    23.         {
    24.             transform.position = new Vector3(0, 0, 0);
    25.         }
    26.        
    27.         if (gameOver == false)
    28.         {
    29.             transform.position = player.transform.position + offset + new Vector3(5, 5, -15);
    30.         }
    31.  
    32.  
    33.     }
    34. }
    35.  
    36.  
    37.  
    38.  
    39.  
    40.  
    41.  
    42.  
    Please help if you can. Thanks.
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    What happens when your player dies? Do you destroy the player? What you can do is use the gameover bool true if statement to set your camera position to prevent it from following the player and it seems you've already done that actually, that's a bit weird.

    Edit: OH! I just realised what you did!

    Code (CSharp):
    1.    transform.position = player.transform.position + offset + new Vector3(5, 5, -15);
    2.         if (gameOver == true)
    3.         {
    4.             transform.position = new Vector3(0, 0, 0);
    5.         }
    6.    
    7.         if (gameOver == false)
    8.         {
    9.             transform.position = player.transform.position + offset + new Vector3(5, 5, -15);
    10.         }
    Should be

    Code (CSharp):
    1.  
    2.         if (gameOver == true)
    3.         {
    4.             transform.position = new Vector3(0, 0, 0);
    5.         }
    6.    
    7.         if (gameOver == false)
    8.         {
    9.             transform.position = player.transform.position + offset + new Vector3(5, 5, -15);
    10.         }
    You were constantly updating the camera's transform position every frame and that piece of code in the update function was ignoring your if statements, your code was otherwise correct though.
     
  3. nomoxd

    nomoxd

    Joined:
    Oct 3, 2019
    Posts:
    2
    Thank you so much!