Look rotation viewing vector is zero...

I keep getting this error and I’m at a loss. The object moves and performs perfectly but this error message keeps popping up. Any insights would be much appreciated.

[20:10:28] Look Rotation viewing vector is zero
UnityEngine.Quaternion:LookRotation (UnityEngine.Vector3)

public class RaptorMove : MonoBehaviour

{
    public float amplitude = 1.0f; // The distance the object will move on either side of its starting position
    public float speed = 1.0f; // The speed at which the object will move

    private Vector3 startPos;
    private Vector3 previousPos;

    void Start()
    {
        // Store the object's starting position
        startPos = transform.position;
        previousPos = startPos;
    }

    void Update()
    {
        // Calculate a new position based on the object's starting position, the amplitude, and a sine wave
    float newX = startPos.x + amplitude * Mathf.Sin(Time.time * speed);
    transform.position = new Vector3(newX, transform.position.y, transform.position.z);
      
    //calculate direction
    Vector3 direction = transform.position - previousPos;

    if(direction.sqrMagnitude == 0f)
    {
        return;
    }
    Quaternion rotation = Quaternion.LookRotation(direction);
    transform.rotation = rotation;
      
    previousPos = transform.position;
    }
}

Maybe if you told us the exact error message we might be able to help. So far, you have just posted a script with no error message.

The title implies it’s the error when you use Quaternion.LookRotation on a directional vector that has zero magnitude.

@Sobewankanobe I think this is probably the issue:

    if(direction.sqrMagnitude == 0f)
    {
        return;
    }
    Quaternion rotation = Quaternion.LookRotation(direction);
    transform.rotation = rotation;
      
    previousPos = transform.position;

Checking floating point numbers for equality is generally not advised. You could try this instead:

if (direction.magnitude > Mathf.epsilon)
{
    Quaternion rotation = Quaternion.LookRotation(direction);
    transform.rotation = rotation;
   
    // etc etc
}

It’s unlikely the direction will be exactly zero, so instead we check if it’s greater than a very small number.

The error message is in their title.

@Sobewankanobe , it looks like you’re trying to protect against the error with a check that the sqrMagnitude is zero. That’s not sufficient. direction.sqrMagnitude < 0.001f would probably work, but honestly, any direction vector should have a nice beefy numerical value, nothing near 0 length. Use Debug.Log() to see if you’re detecting short directions properly.

This fixed it. Thank you sensai!