- Home /
I want my camera to move between two points for a menu
I have a menu and want my camera to move between 2 points to show the game. Here is my script to lerp between two points but nothing is moving. What is wrong?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MoveCamera : MonoBehaviour
{
private Vector3 Target;
private Vector3 cameraStartPosition;
private bool isLerping;
public float Speed;
public void CameraPosition1()
{
isLerping = true;
cameraStartPosition = transform.position;
Target = new Vector3(30f, 2.5f, -5f);
}
public void CameraPosition2()
{
isLerping = true;
cameraStartPosition = transform.position;
Target = new Vector3(-30f, 10f, 200f);
}
void FixedUpdate()
{
if (isLerping)
{
transform.position = Vector3.Lerp(cameraStartPosition, Target, Speed * Time.deltaTime);
}
}
}
Answer by Robotinker · Jul 25, 2020 at 05:39 AM
The lerp function either needs the current position as its starting point or a timer as its third argument. So...
transform.position = Vector3.Lerp(transform.position, Target, Speed * Time.deltaTime);
... which will give you a "strong in" sort of tween, or...
public class MoveCamera : MonoBehaviour
{
...
private float timer;
public float CameraPanTime = 1.5f;
...
void FixedUpdate()
{
if (isLerping)
{
timer += Time.deltaTime;
transform.position = Vector3.Lerp(cameraStartPosition, Target, timer / CameraPanTime);
}
}
}
... which will give you a linear movement.
I tried both of those and nothing the camera stayed in the same position. Is there anything else incorrect with my line of code?
Are you calling the functions CameraPosition() at all?
Oh yes, I must have accidentally deleted the calling functions. Thank you both!
Your answer
Follow this Question
Related Questions
Lerp is not moving 2D Prefab over time. 2 Answers
Lerp not moving 2D object correctly 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is there any way to make lerp work consistantly in update? C# 1 Answer