Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by masterdam17 · Nov 27, 2011 at 01:17 AM · enemymovewaypointspatrol

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);

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image masterdam17 · Nov 27, 2011 at 01:50 AM 0
Share

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 :(

avatar image syclamoth · Nov 27, 2011 at 04:00 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges