Search Unity

Level 2 Camera

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

  1. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    Hello! I have started making the second level for my game. However, the camera doesn't follow the player like it does in the first level, even when I have the camera settings from my first level as a prefab. All of my codes worked for the first level, I just need to get the camera to follow the player again. Here is a picture that I hope will help better explain my problem. If you want to see any of my scripts, please let me know. I have the following scripts that could be useful in this situation: Camera Controller, Player Controller, Level Manager, and Level Loader. Thanks for your help!
     

    Attached Files:

  2. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    I set the smoothing to 5, but the camera still drifts away once I enter Play Mode. The camera seems to focus on the player fine in Edit Mode, but not in Play Mode.
     
  3. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    I'm not sure how to fix the red error: NullReferenceException: Object reference not set to an instance of an object, especially since everything works fine on Level 1, and I don't want to risk changing the codes at all if I can.
     
  4. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    I tried that, but it still won't work. I deleted the suggestion (Thank you for trying), so here is my Player Controller script, and everything, including the camera, works perfectly fine in Level One. Please let me know if you want to see my Camera Controller Script.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.     public float speed = 5f;
    7.     public float jumpSpeed = 8f;
    8.     private float movement = 0f;
    9.     private Rigidbody2D rigidBody;
    10.     public Transform groundCheckPoint;
    11.     public float groundCheckRadius;
    12.     public LayerMask groundLayer;
    13.     private bool isTouchingGround;
    14.     private Animator playerAnimation;
    15.     public Vector3 respawnPoint;
    16.     public LevelManager gameLevelManager;
    17.  
    18.     // Use this for initialization
    19.     void Start()
    20.     {
    21.         rigidBody = GetComponent<Rigidbody2D>();
    22.         playerAnimation = GetComponent<Animator>();
    23.         respawnPoint = transform.position;
    24.         gameLevelManager = FindObjectOfType<LevelManager>();
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         isTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer);
    31.         movement = Input.GetAxis("Horizontal");
    32.         if (movement > 0f)
    33.         {
    34.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    35.             transform.localScale = new Vector2(59.20651f, 59.20651f);
    36.          
    37.         }
    38.         else if (movement < 0f)
    39.         {
    40.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    41.             transform.localScale = new Vector2(-59.20651f, 59.20651f);
    42.         }
    43.         else
    44.         {
    45.             rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
    46.         }
    47.         if (Input.GetButtonDown("Jump") && isTouchingGround)
    48.         {
    49.             rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpSpeed);
    50.         }
    51.         playerAnimation.SetFloat("Speed", Mathf.Abs(rigidBody.velocity.x));
    52.         playerAnimation.SetBool("OnGround", isTouchingGround);
    53.     }
    54.  
    55.     private void OnTriggerEnter2D(Collider2D other) {
    56.         if(other.tag == "FallDetector") {
    57.             gameLevelManager.Respawn();
    58.         }
    59.         if(other.tag == "CheckPoint") {
    60.             respawnPoint = other.transform.position;
    61.         }
    62.     }
    63.  
    64.  
    65. }
     
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    post Camera Controller script.
    and did you tried to place this line in the camera controller script ?
    And show what the error says (doubleclick on it)
     
  6. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    Here is my Camera Controller Script.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraController : MonoBehaviour {
    6.  
    7.     public GameObject player;
    8.     public float offset;
    9.     private Vector3 playerPosition;
    10.     public float offsetSmoothing;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.      
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         playerPosition = new Vector3(player.transform.position.x, player.transform.position.y, transform.position.z);
    20.         if(player.transform.localScale.x > 0f) {
    21.             playerPosition = new Vector3(playerPosition.x + offset, playerPosition.y, playerPosition.z);
    22.         }
    23.         else{
    24.             playerPosition = new Vector3(playerPosition.x - offset, playerPosition.y, playerPosition.z);
    25.         }
    26.  
    27.         transform.position = Vector3.Lerp(transform.position, playerPosition, offsetSmoothing * Time.deltaTime);
    28.     }
    29. }
    30.  
     
  7. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    be sure that the player has tag Player.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class CameraController : MonoBehaviour {
    5.     public GameObject player;
    6.     public float offset;
    7.     private Vector3 playerPosition;
    8.     public float offsetSmoothing;
    9.     // Use this for initialization
    10.     void Start () {
    11. //change players tag to Player
    12.     player = GameObject.FindGameObjectWithTag("Player");
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.         playerPosition = new Vector3(player.transform.position.x, player.transform.position.y, transform.position.z);
    18.         if(player.transform.localScale.x > 0f) {
    19.             playerPosition = new Vector3(playerPosition.x + offset, playerPosition.y, playerPosition.z);
    20.         }
    21.         else{
    22.             playerPosition = new Vector3(playerPosition.x - offset, playerPosition.y, playerPosition.z);
    23.         }
    24.         transform.position = Vector3.Lerp(transform.position, playerPosition, offsetSmoothing * Time.deltaTime);
    25.     }
    26. }
     
    Last edited: Nov 27, 2018
  8. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    That worked, thanks! But now I have another problem. When the player starts to move once spawned in Level 2, they shrink. Is there any way to fix that?
     
  9. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    I now also get a "NullReferenceException: Object reference not set to an instance of an object" notification once I start the game, or at least the second level.
     
  10. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    this can happen because the player isn't in the scene.

    try this for test: still you shouldn't add the follow script to the camera in the levels without your player.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class CameraController : MonoBehaviour {
    5.     public GameObject player;
    6.     public float offset;
    7.     private Vector3 playerPosition;
    8.     public float offsetSmoothing;
    9.     // Use this for initialization
    10.     void Start () {
    11. //change players tag to Player
    12.     player = GameObject.FindGameObjectWithTag("Player");
    13.     }
    14.     // Update is called once per frame
    15.     void Update () {
    16. if (player != null)
    17. {
    18.         playerPosition = new Vector3(player.transform.position.x, player.transform.position.y, transform.position.z);
    19.         if(player.transform.localScale.x > 0f) {
    20.             playerPosition = new Vector3(playerPosition.x + offset, playerPosition.y, playerPosition.z);
    21.         }
    22.         else{
    23.             playerPosition = new Vector3(playerPosition.x - offset, playerPosition.y, playerPosition.z);
    24.         }
    25.         transform.position = Vector3.Lerp(transform.position, playerPosition, offsetSmoothing * Time.deltaTime);
    26.     }
    27. }
    28. }
     
  11. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    I put in the null code that you put in above, but the player still shrunk.
     
  12. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    if (player != null) this line should just prevent the NullReferenceException if the player was missed. It do nothing vs the players shrunk.

    Is the player shrinks only in second level ?
     
  13. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    Yes, the player only shrinks in the second level.
     
  14. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    maybe because of this line: transform.localScale = new Vector2(59.20651f, 59.20651f);

    try to save default value and use it.

    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. }
     
  15. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    Do I put in tmpTransform where you have it written, or is there some other value I'm supposed to put in it's place?
    Because if I put in tmpTransform, I get a Compiler Error Must be Fixed message, and can't enter play mode.
     
  16. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    what should be fixed, which line is it ? Maybe it should be vector3 (or you have missed some lines: check 19, 29, 40, 46)
     
  17. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    The only thing I changed in the code is the tmpTransform sections. If I put in my normal values from before tmpTransform, everything works fine.
     
  18. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    ok, post the script with is giving the error
     
  19. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.     public float speed = 5f;
    7.     public float jumpSpeed = 8f;
    8.     private float movement = 0f;
    9.     private Rigidbody2D rigidBody;
    10.     public Transform groundCheckPoint;
    11.     public float groundCheckRadius;
    12.     public LayerMask groundLayer;
    13.     private bool isTouchingGround;
    14.     private Animator playerAnimation;
    15.     public Vector3 respawnPoint;
    16.     public LevelManager gameLevelManager;
    17.     public AudioClip Running;
    18.     private AudioSource Source;
    19.     public float volLowRange = .5f;
    20.     public float volHighRange = 1.0f;
    21.  
    22.     // Use this for initialization
    23.     void Start()
    24.     {
    25.         rigidBody = GetComponent<Rigidbody2D>();
    26.         playerAnimation = GetComponent<Animator>();
    27.         respawnPoint = transform.position;
    28.         gameLevelManager = FindObjectOfType<LevelManager>();
    29.         Source = GetComponent<AudioSource>();
    30.  
    31.         Vector2 (tmpTransform);
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.         isTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer);
    38.         movement = Input.GetAxis("Horizontal");
    39.         if (movement > 0f)
    40.         {
    41.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    42.             transform.localScale = new Vector2 (tmpTransform);
    43.             Source.PlayOneShot(Running, 1F);
    44.         }
    45.         else if (movement < 0f)
    46.         {
    47.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    48.             transform.localScale = new Vector2(-tmpTransform.x, tmpTransform.y);
    49.         }
    50.         else
    51.         {
    52.             rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
    53.         }
    54.         if (Input.GetButtonDown("Jump") && isTouchingGround)
    55.         {
    56.             rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpSpeed);
    57.         }
    58.         playerAnimation.SetFloat("Speed", Mathf.Abs(rigidBody.velocity.x));
    59.         playerAnimation.SetBool("OnGround", isTouchingGround);
    60.        
    61.     }
    62.  
    63.     private void OnTriggerEnter2D(Collider2D other) {
    64.         if(other.tag == "FallDetector")
    65.         {
    66.             Fall();
    67.  
    68.         }
    69.         if (other.tag == "CheckPoint") {
    70.             respawnPoint = other.transform.position;
    71.         }
    72.     }
    73.  
    74.     private void Fall()
    75.     {
    76.         gameLevelManager.Respawn();
    77.     }
    78. }
     
  20. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    you should declare tmpTransform for the class and not inside Start. Just look at the my script
    there are lines: 19, 29, 40, 46
     
  21. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    Line 29 is within the Start method. Should I put that tmpTransform somewhere else?
     
  22. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    no, in the line 29 the localScale value is stored in the tmpTransform and the line must be in start (awake).
    Just copy&paste the script
     
  23. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    I put in the tmpTransforms, but now the character shrinks even more (only being visible by a microscopic smoke trail I put in) and falls through the ground even though I have a collider on the ground. I just saw your above message. Give me one moment.
     
  24. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    It may have worked, but it looks like I may have to resize my terrain. I'll get back to you soon (hopefully). If it works, thank you so much!
     
  25. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    I think this could work! Thank you so so much!! You've been a huge help Vakabaka!
     
  26. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Good luck :D