I am trying to move my camera in a smooth fashion to view a specific item in my scene, however I can not seem to figure out a way to do this. I will be willing to try just about anything as I have trying to figure out how to do this for about a month now. any help will be extremely appreciated.
Oi, every one of the previous answers is abusing Lerp. This Lerp abuse is a memetic cancer that’s sadly widespread in the Unity community. Read here for more.
The correct way to move something towards a target over multiple frames is to use MoveTowards. It would look something like this:
transform.position = Vector3.MoveTowards(transform.position. endMarker1.position, speed * Time.deltaTime);
Basically just replace “Lerp” with “MoveTowards” and you will no longer be causing your future self suffering and grief.
There are many ways to achieve this. Here is a way to do it
Add a script to your camera and simply make it Lerp
public class cam : MonoBehaviour {
public Transform endMarker = null; // create an empty gameobject and assign in inspector
void Update () {
transform.position = Vector3.Lerp(transform.position, endMarker.position, Time.deltaTime);
}
}
You could also use iTween from asset store. It gives much better control on the ease in/out
Here is what you are looking for clicks on the move button
public List<Transform> camPositionList = new List<Transform>(); //asign all transforms in inspector, including the initial position
private int clickCounter = 0;
private bool clicked;
void Update () {
if (Input.GetMouseButtonDown(0)) // this will rgister a click anywhere on the screen, you need to ray cast on your button
{
clickCounter++;
clicked = true;
clickCounter = Mathf.Clamp(clickCounter, 0, camPositionList.Count-1); //ensures that number of clicks do not exceed the number of transforms in you list else you might get an Out of Range error
}
if (clicked)
{
transform.position = Vector3.Lerp(transform.position, camPositionList[clickCounter].position, Time.deltaTime);
if (transform.position == camPositionList[clickCounter].position) clicked = false;
}
}
Please upvote for the correct answers if it works
In the standard assets there is a script called SmoothFollow.js. It does exactly that
Hello everyone, I used the script chintan_shroff suggested, but made a few modifications to it. This is what I ended up with:`public class Camera_Movement : MonoBehaviour
{
public Transform endMarker1 = null; // create an empty gameobject and assign in inspector
private int count;
void Start()
{
count = 0;
}
void OnGUI()
{
if (GUI.Button (new Rect (100, 100, 100, 100), "Move"))
{
count = 1;
}
}
void Update () {
if (count >= 1)
{
transform.position = Vector3.Lerp(transform.position, endMarker1.position, Time.deltaTime);
}
}
Using this script, is there a way to make it so the camera has several different stops, it moving every time I push the “Move” button?