- Home /
I want to lerp between different changing values
I have various game objects with a boxColliderTrigger (camTargetCollider) and these check if the player is walking into it. Those gameObjects have different locations and i want to move the camera to the position if the player enters the boxColliderTrigger. But the camera is not lerping and snaps to the next location without any smoothness.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FixedRoomCamera : MonoBehaviour
{
public Transform Kamera;
public Collider2D camTargetCollider;
public float accum = 0.0f;
public Vector3 p1, p2;
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag.Equals("Player"))
{
Debug.Log("Trigger2" + p1 + p2);
p1 = Kamera.position;
p2 = camTargetCollider.bounds.center + new Vector3(0,0,-10);
Kamera.transform.position = Vector3.Lerp(p1,p2, Mathf.SmoothStep(0,1,accum));
}
}
// Update is called once per frame
void Update()
{
accum += 1.1f * Time.deltaTime;
}
void Awake()
{
p1 = new Vector3(0,0,-10);
p2 = new Vector3(0,0,-10);
}
}
Answer by Hellium · Feb 21 at 11:29 PM
public Transform Kamera;
public Collider2D camTargetCollider;
private float accum = 0.0f;
private Vector3 p1, p2;
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag.Equals("Player"))
{
accum = 0;
p2 = camTargetCollider.bounds.center + new Vector3(0,0,-10);
}
}
void Update()
{
accum = Mathf.Clamp01(accum + 1.1f * Time.deltaTime);
Kamera.position = Vector3.Lerp(p1,p2, Mathf.SmoothStep(0, 1, accum));
}
void Awake()
{
p1 = p2 = Kamera.position;
}
OR
public Transform Kamera;
public Collider2D camTargetCollider;
[Min(0.001f)] public float transitionDuration = 0.9f;
IEnumerator OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag.Equals("Player"))
{
Vector3 origin = Kamera.position;
Vector3 destination = camTargetCollider.bounds.center + new Vector3(0,0,-10);
for(float t = 0 ; t < transitionDuration ; t += Time.deltaTime)
{
Kamera.position = Vector3.Lerp(origin, destination, Mathf.SmoothStep(0, 1, t / transitionDuration));
yield return null;
}
Kamera.position = destination;
}
}
void Update()
{
// Method can be removed
}
void Awake()
{
// Method can be removed
}
The Second one did the thing, thank you very much @Hellium for you help.
Your answer
Follow this Question
Related Questions
Why cant any operations be performed in camera when loaded from the previous scene? 0 Answers
Slerp Probems 1 Answer
Unity 3D Scene loading Issue in iOS Build 2 Answers
How to make the tower shoot at the one who is closer to the finish line? 0 Answers
Make camera check for multiple things at once in Unity 2D 1 Answer