Hi guys i am working on an AI script for my 2d game, i was wondering if someone could help me basically it works only on one side. the problem is i have a raycast which checks and sees if the player has hit it. if it has it will start walking its way. Now this is working for the right side. but when i added an New raycast using -Vector3.right it does not work the enemy stops working and never fallows. why is this can someone please help me. here is my script thanks you very much in advance MCHALO :)
Script:
var target : Transform;
var MoveSpeed : float;
var RotateSpeed : float;
private var myTransform : Transform;
//[RequireComponent(typeof(PlayerMovement))]
function Awake(){
myTransform = transform;
}
function Start(){
var go : GameObject = GameObject.FindGameObjectWithTag ("Player");
target = go.transform;
}
function Update (){
var EnemyHit : RaycastHit;
Debug.DrawRay (transform.position , Vector3.right * 10, Color.green);
if(Physics.Raycast(transform.position , Vector3.right, EnemyHit , 50)){
print("the Bad guy has hit you ");
Debug.DrawRay (transform.position, Vector3.right * 10, Color.red);
if(EnemyHit.collider.gameObject.tag == "Player"){
// Now we are going to make the enemy look at the target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation
(target.position - myTransform.position) , RotateSpeed * Time.deltaTime);
//Move to target
myTransform.position += myTransform.right * MoveSpeed * Time.deltaTime;
}
Debug.DrawRay (transform.position , -Vector3.right * 10, Color.magenta);
if(Physics.Raycast(transform.position, -Vector3.right, EnemyHit , -50)){
Debug.DrawRay (transform.position , -Vector3.right * 10, Color.green);
if(EnemyHit.collider.gameObject.tag == "player"){
// Now lets set the Enemy to look at the player in its rotation
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation
(target.position - myTransform.position) , RotateSpeed * Time.deltaTime);
myTransform.position -= myTransform.right * -MoveSpeed * Time.deltaTime;
}
}
}
}