- Home /
Smooth Look causing Vector is Zero error...
Hey fellow Unity scripters!
I'm fairly new to the game, but hopefully this isn't an eye-rolling question. My script is actually working fairly well, but since it's all new ground for me, I'm not able to shake this annoying console error. And I just like it when there are no errors. ;)
This script is a basic Enemy AI using a Waypoint array. If the player gets close enough, then the enemy behaves differently (charges and stomps). It has a few animations thrown in there but the problem is coming out of the smooth look function. I spent some time trying to figure out a better method of looking at things outside of:
transform.LookAt();
which gives very sharp turns toward waypoint/player. Eventually I found a nice little snippet that smooths this rotation out, which is:
Quaternion rotation = Quaternion.LookRotation(player.position - _myTransform.position);
_myTransform.rotation = Quaternion.Slerp(_myTransform.rotation, rotation, Time.deltaTime * damping);
This works fine, but when the game begins, it passes out a console error stating: "Look Rotation Viewing Vector is Zero", and a little more detail states: "UnityEngine.Quarternion:LookRotation(Vector3)"
I thought maybe it was trying to get a position before one was loaded in, so I put a little if target !=null at the start, but it didn't help. I've spent a good hour looking into the error and the discussions are a little too advanced for my current C# understanding. So here I am! ^_^
The entire script:
using UnityEngine;
using System.Collections;
public class EnemyMovement : MonoBehaviour {
//Waypoint control.
public Transform[] waypoint;
int currentWaypoint;
//Speed ints.
public int chargeSpeed;
public int patrolSpeed;
//Loops waypoints.
bool loop=true;
//Plug ins.
public Transform player;
public GameObject demonModel;
//Smooth Looking Control.
public float damping = 6.0f;
private Transform _myTransform;
public bool smooth = true;
//Awake.
void Awake()
{
waypoint[0].position = transform.position;
}
// Update is called once per frame
void Update ()
{
//Set the transform.
_myTransform = transform;
//Temp term to streamline code.
Vector3 velocity = rigidbody.velocity;
//If the waypoint is less than the entire length of the waypoint array.
if (currentWaypoint < waypoint.Length)
{
//Temp. variables to streamline code.
Vector3 target = waypoint[currentWaypoint].position;
Vector3 moveDirection = target-transform.position;
Vector3 distFromPlayer = player.position - transform.position;
//Not working. Meant to set the y to 0 so he wont rotate towards waypoint/player.
moveDirection.y = 0;
//Look at target.
if(smooth && distFromPlayer.magnitude>10)
{
Quaternion rotation = Quaternion.LookRotation(target - _myTransform.position);
_myTransform.rotation = Quaternion.Slerp(_myTransform.rotation, rotation, Time.deltaTime * damping);
}
else if (smooth && distFromPlayer.magnitude<10 && !AnxietyController.Hiding)
{
Quaternion rotation = Quaternion.LookRotation(player.position - _myTransform.position);
_myTransform.rotation = Quaternion.Slerp(_myTransform.rotation, rotation, Time.deltaTime * damping);
}
//If the distance between current position and end position is less than 1.
if (moveDirection.magnitude<1)
{
//LK - Crossfade to idle, will be increased when enemy stops.
demonModel.animation.CrossFade("Idle", 0.4F);
//LK - Switch to next waypoint.
currentWaypoint++;
}
//LK - if distance from player is less than 10 and they're not hiding.
if (distFromPlayer.magnitude<10 && !AnxietyController.Hiding)
{
//Set velocity to 0.
velocity = Vector3.zero;
moveDirection.y = 0;
//LK - Look at player.
transform.LookAt(player.position);
//LK - Set velocity to patrol speed.
velocity = (player.position - transform.position).normalized * chargeSpeed;
//LK - Is distance from player is less than 3, then stop moving and crossfade roar animation.
if (distFromPlayer.magnitude < 5)
{
velocity=Vector3.zero;
demonModel.animation.CrossFade("Roar", 0.4F);
}
//if (distFromPlayer.magnitude > 5 && distFromPlayer.magnitude <6)
// {
// velocity=Vector3.zero;
// demonModel.animation.CrossFade("hpaunch", 0.4F);
// }
if (distFromPlayer.magnitude > 6 && distFromPlayer.magnitude <10)
{
velocity = (player.position - transform.position).normalized * chargeSpeed;
demonModel.animation.CrossFade("Walk", 0.4F);
}
}
//Else, play walk and move around as normal.
else
{
demonModel.animation.CrossFade("Walk", 0.4F);
velocity = moveDirection.normalized * patrolSpeed;
}
}
//If waypoint is the same or higher than length of waypoint.
else
{
//And loop is slected.
if(loop)
{
//Reset to first waypoint.
currentWaypoint=0;
}
//If loop is off.
else
{
//Just stop.
velocity=Vector3.zero;
}
}
//Set back the velocity to reset the frame.
rigidbody.velocity= velocity;
}
}
Answer by Americus · Dec 07, 2012 at 09:45 AM
For anybody having the same problem (noticed a few across the web) I found a fix. It is not a fix to the actual problem, which I still don't fully understand yet, but it gets rid of the weird Viewing Vector error code in the console.
if(target != _myTransform.position){
Quaternion rotation = Quaternion.LookRotation(target - _myTransform.position);
_myTransform.rotation = Quaternion.Slerp(_myTransform.rotation, rotation, Time.deltaTime * damping);
}
The idea is to check to make sure both positions are not the same (that is the first position in the Quaternion rotation, and the second one being subtracted). This keeps the code fully functional (as it was only occurring momentarily anyhow) and removes the error. Hope that helps anybody else that has trouble with this.
Answer by kevinv321 · Jul 12, 2013 at 03:04 PM
Hi mate,
I just had the same problem, I am really new to Unity game development (started 2 days ago), but I might know the reason why this happens.
I did debug my Vector 3 values and noticed that it only occurred when y z or x is really small, like 0.00000001. My guess is that somewhere in the process of calculating the rotation, Unity rounds this number to 0.0000 (example) and that means the value is zero. With your solution (in the answers you provided) you simply check if Unity rounded this value. Another way of doing this is:
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
if ( (h > 0.001f || h < -0.001f ) || (v > 0.001f || v < -0.001f) ) {
Rotating(h, v);
Move(h, v);
}
But probably both our ways will result in same effect: preventing from a rounded to 0 value to be used in the rotation script.
Your answer
Follow this Question
Related Questions
Enemy Not Facing target direction (waypoint) correctly when moving between waypoints 1 Answer
Enemy detection while pathfinding through the map 1 Answer
Find enemy problem. 1 Answer
enemy detect player then attack - c# 1 Answer
How to make the enemy move back to its waypoint after it's target killed? 2 Answers