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 monkfallsinsnow · Dec 08, 2013 at 05:56 AM · c#aipathfindingrts

Boids/Flocking Tutorial

Hello everyone. Before I begin, I just want to say how helpful everyone has been guiding me through the various pitfalls that come with learning game development in Unity. Thank you.

Currently, I am trying to expand my knowledge by creating a simple RTS and I ran into something I've never heard of before - boids. Unfortunately, when I give a move command to a group of units, they all fight over the destination vector and either clump together or, if rigid bodies, try to push the single object that occupies the destination vector out of the way. I've been scouring Google in hopes of finding some sort of tutorial on the subject, but I haven't had much luck. I was wondering if anyone has come across anything of the sort. The closest thing I found was this; however, it uses A* instead of Unity's built in pathfinding system. I am a complete novice when it comes to AI programming, so I'm not sure if A* is something I should adopt. I know there are few boid controller and flocking scripts on Unify, but I'd prefer something with a bit of explanation. Any advice would be greatly appreciated. Below is my script in its current state. Thanks again.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class UnitInteraction : MonoBehaviour 
 {    
     bool contactMade;
     bool selected;
     
     GameObject currentSelection;
 
     Vector3 targetDestination;
     
     Shader outline;
     Shader diffuse;
     
     Ray ray;
     RaycastHit hit;
     
     List<GameObject> selectedUnits = new List<GameObject>();
 
     void Start () 
     {
         outline = Shader.Find("Outlined/Silhouetted Diffuse");
         diffuse = Shader.Find("Diffuse");
         selected = false;
     }
 
     void Update () 
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         hit = new RaycastHit();
         
         SelectUnit();
         MoveUnit();
     }
     
     void SelectUnit()
     {
         
         if(Input.GetMouseButtonDown(0))
         {
             if(Physics.Raycast(ray,out hit))
             {
                 currentSelection = hit.collider.gameObject;
                 
                 if(currentSelection.tag == "Unit")
                 {
                     if(selectedUnits.Contains(currentSelection))
                     {
                         currentSelection.renderer.material.shader = diffuse;
                         selectedUnits.Remove(currentSelection);
                         selected = false;
                     }
                     else
                     {
                         selectedUnits.Add(currentSelection);
                         currentSelection.renderer.material.shader = outline;
                         selected = true;
                     }
                     
                     currentSelection = null;
 
                 }
                 else if(hit.collider.gameObject.tag != "Unit")
                 {
                     selectedUnits.Clear();
                     renderer.material.shader = diffuse;
                 }
             }
         }
     }
     
     void MoveUnit()
     {
         if(Input.GetMouseButtonDown(1))
         {
             if(Physics.Raycast(ray,out hit))
             {
                 if(hit.collider.gameObject.tag == "Floor")
                 {
                     targetDestination = hit.point;
 
                     for(int i = 0; i < selectedUnits.Count; i++)
                     {    
                         selectedUnits[i].GetComponent<NavMeshAgent>().SetDestination(targetDestination);
                     }
                     
                 }
             }
         }
     }
 }




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 tanoshimi · Dec 08, 2013 at 07:14 AM 0
Share

If it's explanation you're after, I highly recommend you seek out a copy of "Artificial Intelligence for Games" by $$anonymous$$ilington/Funge - http://www.amazon.co.uk/gp/product/0123747317

Whereas many so-called "A.I." books simply regurgitate the same basic explanations of Reynolds' individual steering behaviours, which you can easily find on the internet anyway, this book actually includes a fairly substantial section on formation movement and "slotting" behaviour to co-ordinate group behaviour. The rest of the book's pretty good too, but it's not cheap, so you might want to ask your local library to order it in.

avatar image Zimbres · Feb 11, 2015 at 02:25 PM 0
Share

this book is really good.

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

19 People are following this question.

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

Related Questions

RTS dynamic formation width and length 0 Answers

How to apply A* algorithm in C#? 2 Answers

Moving multiple AI units as one for RTS game. 1 Answer

How can I prevent a character from ignoring it's waypoint path? 1 Answer

Multiple Cars not working 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