Problem with a Camera Follow Script
Hello, so i got a Script that Follows my Spaceship around, Everything works fine, but if i rotate around the z axis my camera also rotates z and x, but why x, maybe you guys can help me with that.
thank you Maxi
this is the normal state,
and this if i roll,
public Transform target;
public Vector3 defaultDistance = new Vector3(0f,2f,-10f);
public float distanceDamp = 10f;
public float rotationDamp = 2f;
public Vector3 velocity;
bool foundTarget;
private void Start()
{
foundTarget = false;
}
// Use this for initialization
void Update () {
if (!foundTarget)
{
target = GameObject.FindGameObjectWithTag("Ship").transform;
foundTarget = true;
}
}
// Update is called once per frame
void LateUpdate () {
SmoothFollow();
}
void SmoothFollow()
{
Vector3 toPosition = target.position + (target.rotation * defaultDistance);
Vector3 curPosition = Vector3.SmoothDamp(transform.position, toPosition,ref velocity, distanceDamp);
transform.position = curPosition;
transform.LookAt(target, target.up);
}
void SmoothFollowLerp()
{
Vector3 toPosition = target.position + (target.rotation * defaultDistance);
Vector3 curPosition = Vector3.Lerp(transform.position, toPosition, distanceDamp * Time.deltaTime);
transform.position = curPosition;
Quaternion toRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
Quaternion curRotation = Quaternion.Slerp(transform.rotation, toRotation, rotationDamp * Time.deltaTime);
transform.rotation = curRotation;
}
}
Answer by jandd661 · Jul 28, 2017 at 11:50 AM
Is your ship moving in FixedUpdate()
?
https://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html
Hope this helps.
Hey, so my ship isnt moving in FixedUpdate, but it also isnt moved by a rigidbody. But thank you for the fast answer.
Also if i move it in FixedUpdat it starts wiggeling :D
sorry, I made an assumption and we know how those go ;D
Your answer

Follow this Question
Related Questions
Script has been added to my object but it cannot be found using GetComponent() 1 Answer
First person camera flipping by looking up or down too far. 1 Answer
Same script on two objects, only first one works 1 Answer
Joystick Orientation Wrong After Camera Rotation 1 Answer
Camera zoom out relative to player scale (using CinemachineFreeLook) 1 Answer