- Home /
Question by
OGDesigningStudios · Dec 05, 2014 at 11:47 AM ·
distancevector3.distance
Error with rotation and attacking, enemy Ai 2D
I am working on a 2D Platformer game currently and i have developed an enemy ai script. I get an error with this script, when the player gets close to the enemy, It sends this errorNullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) enemy Ai.attack () (at Assets/Scripts/enemy Ai.js:38) enemy Ai.Update () (at Assets/Scripts/enemy Ai.js:26)
When the enemy detects the player, it rotates toward him, but this is not good because he rotates out of view because he turns sideways.
Here is the script
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 3.0;
var Damping = 6.0;
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance > lookAtDistance)
{
}
if (Distance < attackRange)
{
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function attack ()
{
transform.Translate(Vector3.forward * moveSpeed * Time.delteTime);
}
Comment