How to Fix Look Rotation Viewing Vector is Zero Unity Error

What causes the ‘Look rotation viewing vector is zero’ error?

Look rotation viewing vector is zero is an error in unity caused by trying to give Quaternion.LookRotation(..) a vector3 value which is zero length ( / Vector3(0f, 0f, 0f)).

For example this function which sets the rotation of an objects own transform to look in the direction of a moving target object:

void LookTargetMovementDirection (Vector3 targetPosition, Vector3 targetVelocity)
    transform.rotation = Quaternion.LookRotation((targetPosition + targetVelocity) - targetPosition);

If the value of targetVelocity became the look rotation viewing vector would become zero as ((targetPosition + – targetPosition) is just

How to fix the look rotation viewing vector being zero

First you need to find the line of code which is causing the error, you can find this by double clicking the error in the console and you should be taken to the line of code in the script.

Next you can either add some Debug.Log(..) calls to log the value of the Vector3 being assigned to Quaternion.LookRotation(..) or you can use the debugger built into your script editor with a breakpoint on the line of the error, allowing you to inspect the values at runtime. (However this method may be difficult if the look rotation viewing vector isn’t always zero)

How to add debug logging to the vector3 values

Still using the function example used above here’s how you can prepare your function for debugging with debug logs:

void LookTargetMovementDirection(Vector3 targetPosition, Vector3 targetVelocity)
    Vector3 targetLookRotation = (targetPosition + targetVelocity) - targetPosition;

    if(targetLookRotation !={
        transform.rotation = Quaternion.LookRotation(targetLookRotation);
    } else {
        Debug.Log("targetPosition = " + targetPosition);
        Debug.Log("targetVelocity = " + targetVelocity);
        Debug.Log("targetLookRotation = " + targetLookRotation);

The vector3 parameter we were giving Quaternion.LookRotation(..) has been stored in a Vector3 value instead so a comparison to can be ran, this will mean our Debug.Log(..) calls will only happen in instances where the look rotation viewing vector would be zero.

With this debugging we should be able to see in the console which variable is causing the vector3 to be zero, in this example it would be caused by the targetVelocity being zero and we can choose to either not update the rotation while that is zero or restructure our code in a different way depending on the situation.

How to use the Mono Develop debugger with breakpoints to debug a line of code

I’ll explain using Mono Develop as an example but most C# editors are very similar so as long as you have Unity and the script error setup correctly this will work across all script editors.

Mono Develop Debug ButtonTop left of the Mono Develop window you’ll see a play button (or a stop button if the debugger is already running) this controls whether execution will be paused at breakpoints and exception catchpoints.

When the script editor pauses at a breakpoint or an exception catchpoint it allows you to inspect variables in realtime. This allows you to hover over the variables and see their current values.

You can add exception catchpoints from ‘Run > New Exception Catchpoint’ and entering the exception you want to catch under ‘When to Take Action’ and ‘When an exception is thrown’:

Mono Develop add exception catchpoint
However in our case an exception isn’t being logged so we need to manually add a breakpoint to the line we want to inspect, you do this by simply clicking to the left of the line number. When a line had a breakpoint it will be highlighted in red.

Mono Develop how to add breakpoint

Now if we click the script editor debugging play button and enter play mode in Unity; when that line of code is ran the game will pause and Mono Develop will come into focus. At this point you can now hover over the variables and you’ll be shown their current values.

Mono Develop variable inspect

Click the debugger stop button to end debugging or the smaller play button to move on to the next frame. This method is only useful if the look rotation viewing vector is zero error is being logged every frame as the breakpoint will trigger everytime the line is called.

Unity editor canvas bug causing certain unity versions to spam the error

In later versions of Unity 5 and early versions of Unity 2017 there was a bug which would cause the ‘Look rotation viewing vector is zero’ error to spam the console when the width or height of a unity canvas rect transform was 0. A workaround in these versions was to use a very small width/height value such as 0.0001 instead, or otherwise avoid using 0 width/height rects.

This issue was fixed in Unity 2017.2 and the issue tracker report can be read here:

Leave a Reply

Your email address will not be published.