Link To The Problem Video http://www.youtube.com/watch?v=ipAsJbRZFtE&list=HL1367762851
Link To The Eteeski Video http://www.youtube.com/watch?v=wxwQrVH15AM&list=PL7AE076AFAFD3C305
Images:Untitled
My Problem can be shown better in The Video But I’ll Explain It.My enemy Moves Like SlenderMan Right Left And Cant Be Killed.It seems Very Glitchy
if the links dont work please inform me
Scripts
EnemyMovementScript
var currentCell : GameObject;
var playerMovementScript : PlayerMovementScript;
var playerTransform : Transform;
var playerCell : GameObject;
var goalDoor : GameObject;
var shortestPathSoFar : float;
@HideInInspector
var waitToStart : int = 5;
var maxMoveSpeed : float = 5;
var minMoveSpeed : float = 1;
var speedDamage : float = 0.5;
var speedRecover : float = 1;
var currentMoveSpeed : float = 5;
var randomizedCourse : boolean = false;
var randomizeCourseVector : Vector3;
var calculatedNewRandomizeCourseVector : boolean;
var lastPos : Vector3;
var viewAngle : float = 60;
var aware : boolean = false;
var unawareSpeed : float = 1;
function Awake ()
{
shortestPathSoFar = Mathf.Infinity;
playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
playerTransform = GameObject.FindWithTag("Player").transform;
waitToStart = 5;
randomizeCourseVector = transform.position;
lastPos = transform.position;
aware = false;
}
function Update ()
{
if (waitToStart <= 0)
{
playerCell = playerMovementScript.currentCell;
for (var doorCheckingNow : GameObject in currentCell.GetComponent(AIpathCellScript).doors)
{
for (var i : int = 0; i <= doorCheckingNow.GetComponent(AIpathDoorScript).cells.length - 1; i++)
{
if (doorCheckingNow.GetComponent(AIpathDoorScript).cells[i] == playerCell)
if (doorCheckingNow.GetComponent(AIpathDoorScript).doorsToCells[i] < shortestPathSoFar)
{
goalDoor = doorCheckingNow;
shortestPathSoFar = doorCheckingNow.GetComponent(AIpathDoorScript).doorsToCells[i];
}
}
}
shortestPathSoFar = Mathf.Infinity;
}
waitToStart -= 1;
var hits : RaycastHit[];
var anyHit : boolean = false;
if (Vector3.Angle(transform.forward, playerTransform.position - transform.position) < viewAngle / 2 !aware)
{
hits = Physics.SphereCastAll(transform.position, transform.localScale.x / 3, playerTransform.position - transform.position, Vector3.Distance(transform.position, playerTransform.position));
for (var hit : RaycastHit in hits)
{
if (hit.transform.tag == "Level Parts")
anyHit = true;
}
if (!anyHit)
aware = true;
}
if (!aware)
goalDoor = null;
if (!calculatedNewRandomizeCourseVector)
{
randomizeCourseVector = FindRandomSpotInCurrentCell();
calculatedNewRandomizeCourseVector = true;
}
if (goalDoor)
if (!goalDoor.GetComponent(AIpathDoorScript).doorOpen)
goalDoor = null;
if (currentCell != playerCell || playerCell == null || !aware)
{
if (randomizedCourse)
transform.position += (goalDoor.transform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
if (!randomizedCourse)
{
transform.position += (randomizeCourseVector - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, randomizeCourseVector) < transform.localScale.x)
{
if (goalDoor)
randomizedCourse = true;
if (goalDoor == null)
calculatedNewRandomizeCourseVector = false;
}
}
}
if (playerCell == currentCell aware)
transform.position += (playerTransform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
if (currentMoveSpeed < maxMoveSpeed aware)
currentMoveSpeed += speedRecover * Time.deltaTime;
if (currentMoveSpeed > maxMoveSpeed aware)
currentMoveSpeed = maxMoveSpeed;
if (currentMoveSpeed < unawareSpeed !aware)
currentMoveSpeed += speedRecover * Time.deltaTime;
if (currentMoveSpeed > unawareSpeed !aware)
currentMoveSpeed = unawareSpeed;
transform.rotation = Quaternion.LookRotation(transform.position - lastPos);
lastPos = transform.position;
}
function OnTriggerEnter (hitTrigger : Collider)
{
if (hitTrigger.tag == "AIpathCell")
{
currentCell = hitTrigger.gameObject;
randomizedCourse = false;
calculatedNewRandomizeCourseVector = false;
}
}
/*function OnTriggerStay (hitTrigger : Collider)
{
if (hitTrigger.tag == "AIpathCell")
{
currentCell = hitTrigger.gameObject;
}
}*/
function OnTriggerStay (hitTrigger : Collider)
{
if (hitTrigger.tag == "Enemy" hitTrigger.gameObject != gameObject)
{
if (currentMoveSpeed > minMoveSpeed)
currentMoveSpeed -= speedDamage;
transform.position += (transform.position - hitTrigger.transform.position).normalized * 0.1;
}
}
function FindRandomSpotInCurrentCell ()
{
return currentCell.transform.position + (currentCell.transform.rotation * Vector3(Random.Range(currentCell.transform.localScale.x * -0.5,currentCell.transform.localScale.x * 0.5),0,Random.Range(currentCell.transform.localScale.z * -0.5,currentCell.transform.localScale.z * 0.5)));
}
EnemyBodyPartScript
var enemyBody : GameObject;
var addForceVector : Vector3;
var damageMultiplyer : float = 1;
function Update ()
{
if (addForceVector != Vector3.zero !enemyBody.GetComponent(EnemyBodyScript).enabled)
{
if (rigidbody)
rigidbody.AddForce(addForceVector);
else
transform.parent.rigidbody.AddForce(addForceVector);
addForceVector = Vector3.zero;
}
}
EnemyBodyScript
var moveDamp : float = 0.1;
var parentToFollow : Transform;
var parentLastPos : Vector3;
var targetRotation : Quaternion;
var turnSpeed : float = 10;
var velocityX : float;
var velocityZ : float;
var parentScript : EnemyMovementScript;
var walkAnimationString : String;
var runAnimationString : String;
var enemyHealth : float = 1;
var rigidbodies : Rigidbody[];
function Awake ()
{
parentScript = transform.parent.GetComponent(EnemyMovementScript);
animation.wrapMode = WrapMode.Loop;
parentToFollow = transform.parent;
transform.parent = null;
}
function Update ()
{
transform.position.x = Mathf.SmoothDamp(transform.position.x, parentToFollow.position.x, velocityX, moveDamp);
transform.position.z = Mathf.SmoothDamp(transform.position.z, parentToFollow.position.z, velocityZ, moveDamp);
var tempParentCurrentPos : Vector3 = parentToFollow.position;
var tempParentLastPos : Vector3 = parentLastPos;
tempParentCurrentPos.y = 0;
tempParentLastPos.y = 0;
targetRotation = Quaternion.LookRotation(tempParentCurrentPos - tempParentLastPos);
parentLastPos = parentToFollow.position;
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
var hits : RaycastHit[];
hits = Physics.RaycastAll(Ray(transform.position + Vector3(0,100,0), Vector3.up * -1));
for (var i : int = 0; i < hits.length; i++)
{
var hit : RaycastHit = hits[i];
if (hit.transform.gameObject == parentToFollow.gameObject.GetComponent(EnemyMovementScript).currentCell)
transform.position.y = hit.point.y;
}
if (!animation.IsPlaying(walkAnimationString) !parentScript.aware)
animation.Play(walkAnimationString);
if (!animation.IsPlaying(runAnimationString) parentScript.aware)
animation.Play(runAnimationString);
if (enemyHealth <= 0)
{
for (var rigidbodyFor in rigidbodies)
rigidbodyFor.isKinematic = false;
animation.enabled = false;
parentScript.enabled = false;
enabled = false;
}
}
Also when i am near to the enemy,when i almost enter the maze the enemy starts to moving like slender and unit console appear that error(appears only when i amost been in the maze!!) :
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)
EnemyMovementScript.Update () (at Assets/Assets/EnemyMovementScript.js:48)
if you want any other script or something else please inform me quickly !!