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 DasDeer · Jan 23, 2012 at 11:14 AM · aispaceshooterwaypoint

"AI"/Behaviour for a Waypoint system

Hey!

Just like the name implies, i need some help scrpiting some "Ai". Me and a few of my friends is creating a space shooter game, sort of like Star Fox, and we have the basic elements done, except for enemy behaviours. What i want the enemy to do is basically this: When the player is in reach, the enemy will chase the player while firing. When the player is out of their reach, they return to their waypoint patrolling.

So, have you guys got any ideas? This is the waypoint code i use at the moment.

 var waypoint : Transform[];
 var speed : float = 20;
 private var currentWaypoint : int;
 var loop : boolean = true;
 
 function Update(){
 if(currentWaypoint < waypoint.length){
 var target : Vector3 = waypoint[currentWaypoint].position;
 var moveDirection : Vector3 = target - transform.position;
 var velocity = rigidbody.velocity;
 
 transform.LookAt(target);
 if(moveDirection.magnitude < 1){
 currentWaypoint++;
 }
 
 else {
 velocity = moveDirection.normalized * speed;
 }
 }
 else {
 if (loop){
 currentWaypoint=0;
 }
 else{
 velocity = Vector3.zero;
 }
 }
 rigidbody.velocity = velocity;
 }
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 adrenak · Jan 23, 2012 at 01:43 PM 0
Share

You can see the AI.js script in the FPS tutorial if you haven't checked it out yet. Its available on the Unity website, it does exactly the thing that you want and also can be expanded easily for other features you might want to add later to the AI.

avatar image venkspower · Jan 25, 2012 at 09:28 AM 0
Share

Have you written the Gizmo code, in the first place? To place the waypoints?

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by KazeEnji · Jan 23, 2012 at 01:51 PM

For the part where you want them to chase the player when they're in range; you could always use a large sphere collider/trigger. Just make the sphere as large as you want your AI's "sight" to be and make a script that is just an OnTriggerEnter.

Pseudocode:

 if(collision detected with tag "player")
      addForce to enemy and chase player *or you could use move towards if you don't want physics invloved*
     if(player no longer detected)
          Continue moving in last known direction for 5 seconds just because
          Still no player? Return to waypoint script.

Now, you're likely going to run into a problem if you use a trigger event not reactivating if the player passes in a second time. I think when I ran into that problem I used either a disable/enable kind of loop on the Trigger script, or I did a tagging system that referenced the tag on the sphere collider instead of the collider itself...

Hope this helps point you in the right direction.

Later! -Kaze-

Comment
Add comment · Show 1 · Share
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 DasDeer · Jan 25, 2012 at 09:22 AM 0
Share

Thank you. I will try what you and Vatsal said. Hopefully it will work. Thanks again!

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Freelancer of Freespace type Enemy Ai 2 Answers

WayPoints mixed with Raycast 1 Answer

Trying to make AI travel (and face) a waypoint 0 Answers

Way Point Script Change Group 0 Answers

Why isn't a Ai Waypoint Spawning??? 0 Answers


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