Quantcast
Viewing all articles
Browse latest Browse all 1488

Enemy AI Script

I have this script that makes the enemy follow the player, but for some reason i keep getting errors and i don't know why. var target : GameObject; //the enemy's target var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning var range : float=10f; var range2 : float=10f; var stop : float=0; var myTransform : Transform; //current transform data of this enemy function Awake() { myTransform = transform; //cache transform data for easy access/preformance } function Start() { target = target.GameObject; //target the player } function Update () { //rotate to look at the player var distance = Vector3.Distance(myTransform.position, target.position); if (distance<=range2 && distance>=range){ myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); } else if(distance<=range && distance>stop){ //move towards the player myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; } else if (distance<=stop) { myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); } } This is the error i get... NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name) Enemy AI.Update () (at Assets/Scripts/Enemy AI.js:21) How can i fix this?

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>