Search Unity

Camera

Discussion in 'Scripting' started by spearbeard, May 7, 2018.

  1. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    23
    I have made my camera follow my player speed ans position throughout a level. the problem I am having is that when the player gets destroyed the game camera in the unity editor is still running off of the player.

    Code (CSharp):
    1.  
    2. public class PlayerCam : MonoBehaviour {
    3.  
    4.     public Transform player;
    5.     public Vector3 offset;
    6.  
    7.     void Update ()
    8.     {
    9.  
    10.        transform.position = player.position + offset;
    11.     }
    12. }
     
  2. Zero_Xue

    Zero_Xue

    Joined:
    Apr 18, 2012
    Posts:
    116
    Just parent the camera to your player object. Just drag and drop the camera into your player object and position and rotate it accordingly =)
     
  3. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    23
    I have as it follows the players movement it keeps coming up with ! MissingReferanceException the object of type 'Transform' has been destroyed but your are still trying to access it.
     
  4. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    287
    That's because the player is null (since it has been deleted). Change the update code to this to check if the player is null.

    Code (CSharp):
    1. void Update ()
    2.     {
    3.        if(player != null)
    4.        {
    5.               transform.position = player.position + offset;
    6.        }
    7.     }
     
  5. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    23
    heres the player movement script.
    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour {
    2.  
    3.     public Rigidbody rb; //referance to the RigidBody
    4.     public float forwardForce = 2000f; //cube slide speed forward
    5.     public float sidewaysForce = 500f; // cube slide speed sideways
    6.  
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.      
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void FixedUpdate ()
    15.     {
    16.  
    17.         //Input.GetMouseButtonDown((0));
    18.  
    19.         // Adds a forward force to the rigid body
    20.         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
    21.  
    22.         if(Input.GetKey("d"))
    23.         {
    24.             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange); // speed of the cube right
    25.         }
    26.  
    27.         if (Input.GetKey("a"))
    28.         {
    29.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange); // speed of the cube left
    30.         }
    31.  
    32.         if(rb.position.y < -1f)
    33.         {
    34.             FindObjectOfType<GameManager>().EndGame();
    35.         }
    36.     }
    37. }
    38.  
    i posted in the update function and it still come up with the problem
     
  6. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    287
    What problem is still happening the null reference exception? That means that you are trying to use a variable that doesn't contain anything. Double click it so it takes you to the line with the exception and post it here
     
  7. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    23
    Code (CSharp):
    1. public class Score : MonoBehaviour {
    2.  
    3.     public Transform player;
    4.     public Text scoreText;
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.      
    9.  
    10.  
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update ()
    15.     {
    16.         scoreText.text = player.position.z.ToString("0" + "");
    17.     }
    18. }
    19.  
     
  8. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    287
    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if(player != null)
    4.         {
    5.             scoreText.text = player.position.z.ToString("0" + "");
    6.         }
    7.     }
     
  9. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    23
    It works. Thank you
     
    whileBreak likes this.