Look rotation viewing vector is zero

Hi !

I have a script for a game causing an error, and i don’t anderstant why … ?

The script :

void Update()
	{
		float headDisplacement = (head.position - breadcrumbs[0]).magnitude;
		
		if (headDisplacement >= segmentSpacing)
		{
			breadcrumbs.RemoveAt(breadcrumbs.Count - 1); //remove the last breadcrumb
			breadcrumbs.Insert(0, head.position); // add a new one where head is.
			headDisplacement = headDisplacement%segmentSpacing;
		}
		
		if (headDisplacement != 0)
		{
			Vector3 pos = Vector3.Lerp(breadcrumbs[1], breadcrumbs[0], headDisplacement / segmentSpacing);
			segments[0].position = pos;
			segments[0].rotation = Quaternion.Slerp(Quaternion.LookRotation(breadcrumbs[0] - breadcrumbs[1]), Quaternion.LookRotation(head.position - breadcrumbs[0]), headDisplacement / segmentSpacing);
			for (int i = 1; i < segments.Length; i++)
			{
				pos = Vector3.Lerp(breadcrumbs[i + 1], breadcrumbs*, headDisplacement / segmentSpacing);*

_ segments*.position = pos;_
segments.rotation = Quaternion.Slerp(Quaternion.LookRotation(breadcrumbs _- breadcrumbs[i + 1]), Quaternion.LookRotation(breadcrumbs[i - 1] - breadcrumbs), headDisplacement / segmentSpacing);
}
}*_

* }*
Error :
> Look rotation viewing vector is zero
> UnityEngine.Quaternion:LookRotation(Vector3)
> Snake:Update() (at
> Assets/Scripts/Snake.cs:49)
The line causing problem :
segments[0].rotation = Quaternion.Slerp(Quaternion.LookRotation(breadcrumbs[0] - breadcrumbs[1]), Quaternion.LookRotation(head.position - breadcrumbs[0]), headDisplacement / segmentSpacing);

The “viewing vector is zero” usually happens on LookRotation(Vector3.zero). That’s nonsense math – it means you’re accidentally trying to find the angle to look at yourself. The other common place to get it is transform.LookAt(P);, when P is accidentally where you are now.

It’s not really an error, since every direction is sort of looking at yourself, but it means you didn’t plan for everything.

For the snake, it means that either breadCrumb0 is on your head, or breadCrumb1 is at the same place as breadCrumb1. The solution is always to stop and think “what should happen when these are together?” Then usually something like: if(bc[0]==bc[1]) … . Or else you change the code to be sure that head, bc0 and bc1 can never be in the same position.