- Home /
Question by
kollosuss13 · Feb 25, 2020 at 03:48 PM ·
scripting problemcoroutinelerp
[HELP] How to make camera smooth change position while follow object?
Hello. I'm trying to make scale down-up skill. When i press Q object scales down and camera getting closer. Scaling works fine but camera change position not smooth. I'm using this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform Player;
public Vector3 offset;
public Vector3 offset2;
Vector3 currentPosition;
Vector3 finalPosition;
public float transformTime = 0.0f;
public float duration = 1f;
public bool IsTransforming = false;
private ScaleManipulation scaleManipulation;
void Start()
{
scaleManipulation = FindObjectOfType<ScaleManipulation>();
}
// Update is called once per frame
void Update()
{
scaleManipulation = FindObjectOfType<ScaleManipulation>();
if (scaleManipulation.UseSkill)
{
//FollowDown();
transform.position = Player.position + offset2;
}
else if (!scaleManipulation.UseSkill)
{
//FollowUp();
transform.position = Player.position + offset;
}
}
I also Tried to do it with coroutines and lerp, but i don't not how to lerp moving object:
void FollowDown()
{
StartCoroutine("FollowValue");
}
void FollowUp()
{
StartCoroutine("FollowValue2");
}
IEnumerator FollowValue()
{
currentPosition = transform.position;
finalPosition = Player.position + offset2;
float i = transformTime;
IsTransforming = true;
while (i < duration)
{
transform.position = Vector3.Lerp(currentPosition, finalPosition, i / duration * 5);
i += Time.deltaTime * 3;
yield return null;
}
IsTransforming = false;
}
IEnumerator FollowValue2()
{
currentPosition = transform.position;
finalPosition = Player.position + offset;
float i = transformTime;
IsTransforming = true;
while (i < duration)
{
transform.position = Vector3.Lerp(currentPosition, finalPosition, i / duration * 5);
i += Time.deltaTime * 3;
yield return null;
}
IsTransforming = false;
}
}
Comment
Answer by Sxythe · Feb 25, 2020 at 03:55 PM
You could use Cinemachine from the Package Manager and use a virtual camera. It's quite simple and pretty smooth.
Your answer
Follow this Question
Related Questions
Lerp coroutine not executing 0 Answers
rotate object by list values 0 Answers
Coroutine not stopping with rotation (stuck on infinite loop). 1 Answer