Search Unity

Camera doesn't change position smoothly

Discussion in 'Scripting' started by MitjaHD, Nov 12, 2018.

  1. MitjaHD

    MitjaHD

    Joined:
    Sep 30, 2018
    Posts:
    64
    I'm trying to focus camera on enemy sprite once it hits the ground, however using Vector3.Lerp gives me headaches because it doesn't work as I expected.

    Code (CSharp):
    1. if(collision.gameObject.CompareTag("EndGround"))
    2.         {
    3.             GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;
    4.             GetComponent<Rigidbody2D>().angularDrag = 1000;
    5.             ChangeCamera = true;
    6.         }
    7.  
    Code (CSharp):
    1.  
    2. void FixedUpdate () {
    3.  
    4.         if(ChangeCamera)
    5.         {
    6.             GameObject.Find("Main Camera").GetComponent<CameraFollow>().enabled = false;
    7.             PlayerCamera.transform.position = Vector3.Lerp(player.transform.position, transform.position - offset, Time.deltaTime * 3f);
    8.         }
    9.     }
    Script is attached to enemy and when he hits the ground the bool sets to true in FixedUpdate. First I disable camera follow script so it's not following my player anymore, then it should move the the current position of player. If I set the time in Lerp to 1 or more it goes there immedietly, but if it's less than one then it doesn't even reach the target. If it's 0.5 then it goes halfway there. I guess I don't understnad Lerp.
     
  2. MitjaHD

    MitjaHD

    Joined:
    Sep 30, 2018
    Posts:
    64
    Nevermind I fixed it. I used player's position for starting position, instead of camera's which is attached to this player. Such a stupid mistake...