- Home /
Question by
onur_o · Sep 25, 2020 at 06:27 PM ·
rotationquaternionslerpmain camera
A Problem With Quaternion.Slerp
I wrote a code block to rotate the main camera. However, the result is not what I want.
public Transform mainCamera;
public Vector3 toVector;
public float speed;
private float timeCount = 0.0f;
private Quaternion from;
private Quaternion to;
private void Awake()
{
mainCamera = GameObject.FindGameObjectWithTag("MainCamera").transform;
}
private void OnEnable()
{
from = mainCamera.localRotation;
to = Quaternion.Euler(toVector);
}
void Update()
{
mainCamera.localRotation = Quaternion.Slerp(from, to, timeCount);
timeCount = timeCount + speed * Time.deltaTime;
if (timeCount > 1.0f)
{
timeCount = 0.0f;
this.enabled = false;
}
}
Result;
How to fix it?
inspector-1.png
(9.9 kB)
inspector-3.png
(12.7 kB)
Comment
Answer by Kaart · Sep 25, 2020 at 08:00 PM
I modified your script a little. Seems to work fine now. You might want to consider using WaitForSeconds (https://docs.unity3d.com/ScriptReference/WaitForSeconds.html) instead but for now this seems to work fine. Hope this helps.
public Transform mainCamera;
public Vector3 toVector;
public float speed;
private float timeCount = 0.0f;
private Quaternion from;
private Quaternion to;
private bool rotating;
private void Awake()
{
mainCamera = GameObject.FindGameObjectWithTag("MainCamera").transform;
rotating = true;
}
private void OnEnable()
{
from = mainCamera.localRotation;
to = Quaternion.Euler(toVector);
}
void Update()
{
if (rotating)
{
mainCamera.localRotation = Quaternion.Slerp(from, to, timeCount);
timeCount = timeCount + speed * Time.deltaTime;
if (timeCount >= 1.0f)
{
rotating = false;
mainCamera.localRotation = to;
}
}
}
Your answer
Follow this Question
Related Questions
How can I use lerp to rotate an object multiple times? 2 Answers
Quaternion Euler and Slerp: subtracting degrees only works up to a certain angle 1 Answer
Rotation direction in coroutine 2 Answers
Slerp Finishes Rotation When Progress is 0.5f 1 Answer
Selecting the rotation direction for Quaternion.Slerp 1 Answer