- Home /
AI help please :)
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;
}
}
}
}
Why do you have so much space between every line of code? The scrolling makes me crazy! :P
lol you know what i have no idea when i pasted it everything was fine lol. but as soon as i pressed the 101010 button everything went like this lol
Answer by loramaru · May 08, 2011 at 07:04 AM
Your problem is that the left raycast check is nested inside the right raycast check, so it will never fire since they can't both be true at once. Note the nesting problem in the cleaned up version of Update() below. Being able to easily spot errors like this is one of the many reasons well-indented code is essential.
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;
}
}
}
}
That does not work as well i have done the something before but still not progress :) thanks for helping me at lest :)
Well then looking at the code itself, my next thought would be that I don't believe you want -50 on your left Raycast. Try positive 50 ins$$anonymous$$d.
Fixed it thanks it was something you said in your answer it was the brackets that gave me the problem :).
just update the answer so after each raycast there should be to closing brackets because there are two ifs in each ray cheers :)
Answer by Fido77 · May 08, 2011 at 07:54 AM
why is one magenta and the other red? sorry, i'm pretty newbish on this, but that was confusing me. from what it looks like you doing the same thing just on opposite sides. why is the red changed to magenta?
Those are the just the colours of the lines when they will show in the game scene mode :). it helps you see where your ray is starting from :)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
AI Enemy Follow Player 2 Answers
Is it possible to make enemies Open doors? 2 Answers
Enemy Follow AI problem 0 Answers
How do you move did with [iTWEEN] the enemy has a component of [Character Controller]? 0 Answers