- Home /
Enemies walk to waypoints and stop in specific ones
Hey thats the point... i just have a multiple waypoint set for every enemy, and i now included some code to make them stop in Stay tagged ones... but it just glitches out .. :( this is the new code for it
var Waypoint:Transform [];
var speed = 5;
var currentWaypoint:int;
var loop:boolean = true;
var viewAngle:float = 360;
var InDistance:Vector3;
var PlayerPos:Vector3;
var InDistanceMag:float;
var Seen:boolean = false;
var InAngle:float;
var DistanceToSpot:float = 7;
var Alarm:GameObject;
var SubjectInSight:Transform;
var Lives:int = 4;
var Hit:AudioClip[];
var TimeToStay:float;
function Awake(){
Waypoint[0] = transform;
Alarm = GameObject.Find("Alarm");
}
function Update () {
PlayerPos = GameObject.Find("Player").transform.position;
if(currentWaypoint < Waypoint.Length && Waypoint[currentWaypoint].tag != "Stay"){
var target:Vector3 = Waypoint[currentWaypoint].position;
var moveDirection:Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1){
currentWaypoint ++;
}
else if(moveDirection.magnitude > 1 ){
velocity = moveDirection.normalized * speed *Time.deltaTime;
}
} if(Waypoint[currentWaypoint].CompareTag("Stay")){
Stay();
}
if(currentWaypoint > Waypoint.Length){
if(loop){
currentWaypoint = 0;
}
else{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(Vector3(target.x,transform.position.y,target.z));
THE CODE CONTINUES, BUT IT JUST ISNT IMPORTANT UNTIL
function Stay(){
currentWaypoint ++;
print("Yielding");
yield WaitForSeconds(TimeToStay);
}
Ok thats the new code, but the one that worked till the moment was :
var Waypoint:Transform [];
var speed = 5;
var currentWaypoint:int;
var loop:boolean = true;
var viewAngle:float = 360;
var InDistance:Vector3;
var PlayerPos:Vector3;
var InDistanceMag:float;
var Seen:boolean = false;
var InAngle:float;
var DistanceToSpot:float = 7;
var Alarm:GameObject;
var SubjectInSight:Transform;
var Lives:int = 4;
var Hit:AudioClip[];
function Awake(){
Waypoint[0] = transform;
Alarm = GameObject.Find("Alarm");
}
function Update () {
PlayerPos = GameObject.Find("Player").transform.position;
if(currentWaypoint < Waypoint.Length){
var target:Vector3 = Waypoint[currentWaypoint].position;
var moveDirection:Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1){
currentWaypoint ++;
}
else{
velocity = moveDirection.normalized * speed *Time.deltaTime;
}
}
else{
if(loop){
currentWaypoint = 0;
}
else{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(Vector3(target.x,transform.position.y,target.z));
//CHECK SPOTTING PLAYER
InDistance = PlayerPos - transform.position;
InDistance.y = 0;
InAngle = Vector3.Angle(PlayerPos - transform.position,transform.forward);
if(InDistance.magnitude <= DistanceToSpot && InAngle <= viewAngle)
{
var hit:RaycastHit;
Physics.Raycast(transform.position,Vector3.Normalize(PlayerPos - transform.position),hit,50);
if(hit.collider.CompareTag("wall")){
Seen = false;
}
else if(hit.collider.CompareTag("Player")){
Seen = true;
}
InDistanceMag = InDistance.magnitude;
}
//ACTIVATE ALARM!!
if(Seen==true){
Alarm.SendMessage("Trigger");
stopmoving();
}
}
function OnCollisionEnter(other:Collision){
if(other.gameObject.tag == ("Bullet")){
Lives -= 1;
audio.clip = Hit[Random.Range(0,2)];
audio.Play();
Destroy(other.gameObject);
if(Lives == 0){
Destroy (gameObject);
}
}
}
function stopmoving(){
Instantiate(SubjectInSight,Vector3(transform.position.x,transform.position.y + 1,transform.position.z),Quaternion.identity);
transform.LookAt(PlayerPos);
Destroy(this);
}
i got to make the enemy print staying when the next waypoint is tagged stay, but it actually then swaps to the next waypoint right away... without even passing through the stay one :(
Ok, you see that thing you just did there? With the humungous code dump? Don't do that. Also, use the preview feature when you are posting your question to make sure it is well formatted- nobody wants to read something where half the lines aren't even fixed-width, and badly indented.
Your answer
Follow this Question
Related Questions
Kind of Lag in movement 2 Answers
Why my enemy starts to bump between two waypoints during its patrolling 0 Answers
Enemy Patrol - Random Walk 1 Answer
How do I make the enemy stop walking and shoot the player? 1 Answer
Monster Patrolling 1 Answer