Search Unity

Automatic camera movement 2D [C#] [Solved]

Discussion in 'Scripting' started by Thomas90, Jul 26, 2016.

  1. Thomas90

    Thomas90

    Joined:
    Mar 30, 2015
    Posts:
    35
    Hi guys!

    I'm making a 2D pixel platformer, and I want some of the levels to have the camera move automatically to the right along the x-axis.
    I've managed to do so, and it works as expected. But, however, my problem is when the player is being respawned. When the player is being respawned, the camera jumps back to the start position (set by a vector 3). But it seems like the camera is now locked on to that position, and therefore overrules the transform.translate that initially makes all the movement of the camera.

    Any ideas on how to work around this?



    Code (CSharp):
    1.  public class CameraAutoScroll : MonoBehaviour {
    2.  
    3. public LevelManager theLevelManager;
    4.  
    5. public float cameraScrollSpeed;
    6.  
    7. public bool cameraRespawn;
    8.  
    9. public Vector3 startPosition;
    10.  
    11. public float waitToScroll;
    12.  
    13. //Usethisfor initialization
    14. void Start ()
    15. {
    16.        theLevelManager = FindObjectOfType <LevelManager> ();
    17.  
    18.        startPosition = transform.position;
    19. }
    20.  
    21. //Updateiscalledonceper frame
    22. void Update ()
    23. {
    24.        transform.Translate (cameraScrollSpeed, 0f, 0f);
    25.  
    26.        if (theLevelManager.playerRespawn)
    27.        {
    28.              cameraRespawn = true;
    29.        }
    30.  
    31.        if (cameraRespawn)
    32.        {
    33.             StartCoroutine ("CameraCoRoutine");
    34.        }
    35. }
    36.  
    37. public IEnumerator CameraCoRoutine ()
    38. {
    39.        transform.position = startPosition;
    40.  
    41.        yield return new WaitForSeconds (waitToScroll);
    42.  
    43.        transform.Translate (cameraScrollSpeed, 0f, 0f);
    44. }
    45. }
    46.  
     
  2. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Do you want the camera to follow the player or to stay in one spot until they get to another area and then change to that area camera position?
     
  3. Thomas90

    Thomas90

    Joined:
    Mar 30, 2015
    Posts:
    35
    I want the camera to move independently from the player. But when the player respawns, I want the camera to jump to that position and start moving independently again. Hope that makes sense!
     
  4. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    You never set "cameraRespawn" back to false
     
  5. Thomas90

    Thomas90

    Joined:
    Mar 30, 2015
    Posts:
    35
    Hmm, it is clearly that that is what is causing the problem, but I can't seem to get it right.
    I tried to set cameraRespawn back to false both as an "else" function inside update, and I tried to do it inside the CoRoutine. None of those did work. Is there a way to make the transform.position "free" from what I've previously stated? As in something like transform.position = null?

    I am by far not a programmer, so I need to being told everything in detail. Sorry about that! Haha.
     
  6. pienelio

    pienelio

    Joined:
    Jul 26, 2016
    Posts:
    4
    Maybe you need to set theLevelManager.playerRespawn to false too? Also transform.position is a struct and can never be null.
     
  7. Thomas90

    Thomas90

    Joined:
    Mar 30, 2015
    Posts:
    35
    Hi again! I've figured it out (meaning, my very clever roommate did).





    Code (CSharp):
    1.  public class CameraAutoScroll : MonoBehaviour {
    2.  
    3. public LevelManager theLevelManager;
    4.  
    5. public float cameraScrollSpeed;
    6.  
    7. public bool cameraRespawn;
    8.  
    9. public Vector3 startPosition;
    10.  
    11. public float waitToScroll;
    12.  
    13.  
    14. //Usethisfor initialization
    15. void Start ()
    16. {
    17.       theLevelManager = FindObjectOfType <LevelManager> ();
    18.  
    19.       startPosition = transform.position;
    20. }
    21.  
    22. //Updateiscalledonceper frame
    23. void Update ()
    24. {
    25.       if (theLevelManager.playerRespawn)
    26.       {
    27.             transform.position = startPosition;
    28.             StartCoroutine ("CameraCoRoutine");
    29.       }
    30.  
    31.       else
    32.       {
    33.             transform.Translate (cameraScrollSpeed, 0f, 0f);
    34.       }
    35. }
    36.  
    37.  
    38. public IEnumerator CameraCoRoutine ()
    39. {
    40.        yield return new WaitForSeconds (waitToScroll);
    41.        theLevelManager.playerRespawn = false;
    42. }
    43. }
    44.  
    45.  
    46.