Search Unity

Strange behaviour of camera following script when player is destroyed and respawned

Discussion in '2D' started by xqtr123, Mar 14, 2021.

  1. xqtr123

    xqtr123

    Joined:
    Jun 7, 2014
    Posts:
    20
    Hi!
    I'm making a 2D game where when the player runs off a cliff he will be respawned. This is done like this:

    Code (CSharp):
    1. public class GameMaster : MonoBehaviour
    2. {
    3.     public static GameMaster gm;
    4.     public Transform playerPrefab;
    5.     public Transform spawnPoint;
    6.     public int spawnDelay = 2;
    7.     private bool hasSpawned = false;
    8.  
    9.     void Start()
    10.     {
    11.         if (gm == null)
    12.         {
    13.             gm = this;
    14.         }
    15.     }
    16.  
    17.     public IEnumerator RespawnPlayer(Player player)
    18.     {
    19.         yield return new WaitForSeconds(spawnDelay);
    20.         Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
    21.         Destroy(player.gameObject);
    22.     }
    23.  
    24.     public static void KillPlayer(Player player)
    25.     {
    26.         gm.StartCoroutine(gm.RespawnPlayer(player));
    27.     }
    28. }
    This code works, but when the player is respawned the camera should follow, which it does but it messes up the view like below:

    upload_2021-3-14_18-16-48.png

    The camera follow script has the following code:

    Code (CSharp):
    1. public class Camera2DFollow : MonoBehaviour
    2. {
    3.     public Transform target;
    4.     public float damping = 1;
    5.     public float lookAheadFactor = 3;
    6.     public float lookAheadReturnSpeed = 0.5f;
    7.     public float lookAheadMoveThreshold = 0.1f;
    8.     public int m_OffsetY;
    9.     public float yPosRestriction = -1;
    10.  
    11.     private float m_OffsetZ;
    12.     private Vector3 m_LastTargetPosition;
    13.     private Vector3 m_CurrentVelocity;
    14.     private Vector3 m_LookAheadPos;
    15.  
    16.     private int count = 0;
    17.  
    18.     float nextTimeToSearch = 0;
    19.  
    20.     // Use this for initialization
    21.     private void Start()
    22.     {
    23.         if (target == null)
    24.         {
    25.             return;
    26.         }
    27.  
    28.         m_LastTargetPosition = target.position;
    29.         m_OffsetZ = (transform.position - target.position).z;
    30.         transform.parent = null;
    31.     }
    32.  
    33.  
    34.     // Update is called once per frame
    35.     private void Update()
    36.     {
    37.         if (target == null)
    38.         {
    39.             target = FindPlayer();
    40.         }
    41.        
    42.         // only update lookahead pos if accelerating or changed direction
    43.         float xMoveDelta = (target.position - m_LastTargetPosition).x;
    44.  
    45.         bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
    46.  
    47.         if (updateLookAheadTarget)
    48.         {
    49.             m_LookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
    50.         }
    51.         else
    52.         {
    53.             m_LookAheadPos = Vector3.MoveTowards(m_LookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);
    54.         }
    55.  
    56.         Vector3 aheadTargetPos = target.position + m_LookAheadPos + Vector3.forward * m_OffsetZ;
    57.         Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref m_CurrentVelocity, damping);
    58.  
    59.         newPos = new Vector3(newPos.x, Mathf.Clamp(newPos.y, yPosRestriction, Mathf.Infinity), newPos.z);
    60.  
    61.         transform.position = new Vector3(newPos.x, aheadTargetPos.y + m_OffsetY, newPos.z);
    62.  
    63.         m_LastTargetPosition = target.position;
    64.     }
    65.  
    66.     private Transform FindPlayer()
    67.     {
    68.         Transform searchResult = GameObject.FindGameObjectWithTag("Hero").transform;
    69.  
    70.         if (searchResult == null)
    71.         {
    72.             Debug.LogWarning("Player object could not be found.");
    73.             return null;
    74.         }
    75.         else
    76.         {
    77.             return searchResult;
    78.         }
    79.     }
    80. }
    Can anyone tell why this is happening?