How to move a camera smoothly to a fixed position and back using C#?
Hi, everybody! I am new at Unity 3D. And I was trying to zoom my perspective camera by moving it with the z-axis. All I need to do - is to make it move nice and smooth to a fixed position. I was trying to use Lerp thing but it doesn't work as it supposed to, because if I put low speed, it doesn't move to the end position, it stops like if it has no time anymore. If anyone knows how to make it work, it would be so cool if you help me out!! I've spent like 2 days trying to make this "simple" thing and I've read tons of Documentations. Thanks in advance! Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera_perspective_dynamic : MonoBehaviour {
public float camSizeMaxLimit = -14.0f;
public float camSizeMinLimit = -10.0f;
public float camSpeed = 0.2f;
public Camera Cam;
public Vector3 camera_position_max = new Vector3(0.0f, 0.0f, 0.0f);
public Vector3 camera_position_min = new Vector3(0.0f, 0.0f, 0.0f);
// Use this for initialization
void Start()
{
Cam = GameObject.Find("Main Camera").GetComponent<Camera>();
}
void Update()
{
camera_position_max = new Vector3(transform.position.x, transform.position.y, camSizeMaxLimit);
camera_position_min = new Vector3(transform.position.x, transform.position.y, camSizeMinLimit);
if (Trigger_CameraPersp_Zoom.zoom == true)
{
ZoomIn();
}
if (Trigger_CameraPersp_Zoom.zoom == false)
{
ZoomOut();
}
}
void ZoomIn()
{
Cam.transform.position = Vector3.Lerp(transform.position, camera_position_max, Time.deltaTime * camSpeed);
}
void ZoomOut()
{
Cam.transform.position = Vector3.Lerp(transform.position, camera_position_min, Time.deltaTime * camSpeed);
}
}
Your answer
Follow this Question
Related Questions
Mouse Scroll not working on Camera (But can scroll the player) 0 Answers
How to create camera scroll and zoom? 0 Answers
Edit Z position of third-person style camera 0 Answers
Make camera follow and look at object at the same time 0 Answers
Camera rotation problem: Keeps reseting to initial position 0 Answers