- Home /
How do I make a smooth animation in unity
I am trying to make a map in unity in which you can zoom on the player when you press the F key. For now, I just made a simple code that allows you to move the map with WASD or arrows keys you can zoom in and out with the mouse scroll wheel and by pressing F the camera focuses on the position of the player but the animation is instantaneous. What do I have to do to move the camera progressively? Here it is the code...
public class BigMapMov : MonoBehaviour
{
public Camera bigMapCam;
public float moveSpeed;
public GameObject player;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
MoveCam();
if (Input.GetAxis("Mouse ScrollWheel") < 0f) // diminuisce
{
bigMapCam.orthographicSize += 50;
}
else if (Input.GetAxis("Mouse ScrollWheel") > 0f) // ingrandisce
{
bigMapCam.orthographicSize -= 50;
}
if (bigMapCam.orthographicSize > 3500)
{
bigMapCam.orthographicSize = 3500;
}
if (bigMapCam.orthographicSize < 50)
{
bigMapCam.orthographicSize = 50;
}
}
void MoveCam()
{
//Focus on the player
if (Input.GetKeyDown(KeyCode.F))
{
FocusOnPlayer();
}
//Get the input
float camX = Input.GetAxisRaw("Horizontal");
float camZ = Input.GetAxisRaw("Vertical");
//speed fix
if (3500 <= bigMapCam.orthographicSize && bigMapCam.orthographicSize > 2400)
{
moveSpeed = 2f;
}
else if (2400 <= bigMapCam.orthographicSize && bigMapCam.orthographicSize > 1200)
{
moveSpeed = 4f;
}
else if (1200 <= bigMapCam.orthographicSize && bigMapCam.orthographicSize > 50)
{
moveSpeed = 8f;
}
//camera movement
if (bigMapCam.orthographicSize <= bigMapCam.transform.position.x && (7000 - bigMapCam.orthographicSize) >= bigMapCam.transform.position.x)
{
bigMapCam.transform.position += new Vector3(camX * moveSpeed, 0, 0);
}
if (bigMapCam.orthographicSize <= bigMapCam.transform.position.z && (7000 - bigMapCam.orthographicSize) >= bigMapCam.transform.position.z)
{
bigMapCam.transform.position += new Vector3(0, 0, camZ * moveSpeed);
}
if (bigMapCam.transform.position.x > (7000 - bigMapCam.orthographicSize))
{
if ((bigMapCam.transform.position.x - (7000 - bigMapCam.orthographicSize)) >= 10)
{
bigMapCam.transform.position += new Vector3(-15, 0, 0);
}
else
{
bigMapCam.transform.position += new Vector3(-1, 0, 0);
}
}
if (bigMapCam.transform.position.x < (bigMapCam.orthographicSize))
{
if ((bigMapCam.transform.position.x - bigMapCam.orthographicSize) <= -10)
{
bigMapCam.transform.position += new Vector3(15, 0, 0);
}
else
{
bigMapCam.transform.position += new Vector3(1, 0, 0);
}
}
if (bigMapCam.transform.position.z > (7000 - bigMapCam.orthographicSize))
{
if ((bigMapCam.transform.position.z - (7000 - bigMapCam.orthographicSize)) >= 10)
{
bigMapCam.transform.position += new Vector3(0, 0, -15);
}
else
{
bigMapCam.transform.position += new Vector3(0, 0, -1);
}
}
if (bigMapCam.transform.position.z < (bigMapCam.orthographicSize))
{
if ((bigMapCam.transform.position.z - bigMapCam.orthographicSize) <= -10)
{
bigMapCam.transform.position += new Vector3(0, 0, 15);
}
else
{
bigMapCam.transform.position += new Vector3(0, 0, 1);
}
}
}
void FocusOnPlayer()
{
if (player.transform.position.x <= 50)
{
bigMapCam.orthographicSize = 50;
bigMapCam.transform.position += new Vector3((50 - bigMapCam.transform.position.x), 0, 0);
}
else if (player.transform.position.x > 50 && player.transform.position.x < 6950)
{
bigMapCam.orthographicSize = 50;
bigMapCam.transform.position += new Vector3((player.transform.position.x - bigMapCam.transform.position.x), 0, 0);
}
else if (player.transform.position.x >= 6950)
{
bigMapCam.orthographicSize = 50;
bigMapCam.transform.position += new Vector3((6950 - bigMapCam.transform.position.x), 0, 0);
}
if (player.transform.position.z <= 50)
{
bigMapCam.orthographicSize = 50;
bigMapCam.transform.position += new Vector3(0, 0, (50 - bigMapCam.transform.position.z));
}
else if (player.transform.position.z > 50 && player.transform.position.z < 6950)
{
bigMapCam.orthographicSize = 50;
bigMapCam.transform.position += new Vector3(0, 0, (player.transform.position.z - bigMapCam.transform.position.z));
}
else if (player.transform.position.z >= 6950)
{
bigMapCam.orthographicSize = 50;
bigMapCam.transform.position += new Vector3(0, 0, (6950 - bigMapCam.transform.position.z));
}
}
}
Answer by Mrintoxx · Aug 03, 2020 at 10:48 AM
Maybe check Vector3.Lerp to smooth the movement of the camera. you have the doc here
replace :
bigMapCam.transform.position += new Vector3((50 - bigMapCam.transform.position.x), 0, 0);
by :
bigMapCam.transform.position = Vector3.Lerp(startPos, endPos, timeTravel);
Your answer
Follow this Question
Related Questions
Trouble with logic related to my camera smoothing 0 Answers
Multipurpose CameraRig - Smooth Movement - how to achieve? 1 Answer
Camera Smoothing from pos A - pos B in Unity 2D 0 Answers
Adjust camera orientation through code 0 Answers
How to move camera so that the finger points to the same spot? 1 Answer