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;
}
}