- Home /
transform.rotation = Quaternion.Slerp "freaks out doesn't acutely track game object.
this should work, after 4 seconds the camera should look at and track my object, instead it freaks out to the side and jitters until you stop the scene.
using UnityEngine;
using System.Collections;
public class CameraControlla : MonoBehaviour {
public float TimerOne = 0f;
private float rotationSpeed = 12;
public bool CameraCall1 = false;
public bool CameraCall2 = false;
public GameObject target;
public GameObject target2;
// Update is called once per frame
void Update () {
TimerOne += 1 * Time.deltaTime;
if (TimerOne > 4) {
CameraCall1 = true;
}
if (TimerOne > 8) {
CameraCall1 = true;
}
if(CameraCall1 == true){
// Track
transform.rotation = Quaternion.Slerp(target.transform.rotation, Quaternion.LookRotation(target.transform.position - transform.position ), Time.deltaTime * rotationSpeed);
}
if(CameraCall2 == true){
// Track
transform.rotation = Quaternion.Slerp(target2.transform.rotation, Quaternion.LookRotation(target2.transform.position - transform.position ), Time.deltaTime * rotationSpeed);
}
}
}
Answer by Yujingping · Aug 16, 2016 at 02:35 AM
Hey there. Let's focus on this sentence:
transform.rotation = Quaternion.Slerp(target.transform.rotation, Quaternion.LookRotation(target.transform.position - transform.position ), Time.deltaTime * rotationSpeed);
What this does, is to set the rotation of your camera from the rotation of your TARGET to the direction from your camera to the target. Obviously this is not what you wanna to do, so why does this cause a freaking effect?
Let's split the process and analyze step by step: 1. The rotation of your camera is set to the spherical interpolation of the rotation of your target and the direction that you actually want to face. Namely, now the camera's rotation is roughly the same with your TARGET. And you can see a transition from the original rotation to the new rotation. 2. The Update function is called again, but since the rotation of your target actually doesn't change at all, the rotation of your camera is set to that of your target again. And then, transit again.
Hence, the view of camera freaks and doesn't stop trembling at a "strange angle".
So where is your error? Simply change that sentence to this:
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.transform.position - transform.position ), Time.deltaTime * rotationSpeed);
Now the rotation of the camera is really updated based on its own rotation, and you can see a smooth transition from its original angle to another. Basically when you wanna to Interpolate a value and change it continuously, the value should appear in the first parameter of the Lerp function to make sense.
Lastly just mention that your CameraCall2 will never be set in the script provided.
Please feel free for further questions.
GL & HF!
Your answer
![](https://koobas.hobune.stream/wayback/20220612090906im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
need help on prefab spawning 1 Answer
Script error about the semicolon. 1 Answer
instantiating prefabs from array 1 Answer
What is Mono? Is it a compiler? A language? Or what? 3 Answers
What is Vector3, Vector4, etc.? 2 Answers