Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by KINDOK · Feb 01, 2016 at 01:23 PM · c#2d gameaiprogrammingtopdown

[Problem] Need help with this! C#

I'm making a top-down game and the problem come when I add the bots. I have a simple AI script to indicate the bots 3 possible targets: Normal food, player and another bots. To recognize each of the targets, I indicated it with tags: "food", "Player" and "bot" It work perfect with "food" and "Player", but when it touch with another one with the same target, it disappear.

How I can differentiate the bots with a String instead of a tag? There is my script:

 using UnityEngine;
 using System.Collections;
  
 public class bot_profile : MonoBehaviour
 {
     public string Name = "botname";
     public GameObject Skin;
     public GameObject dmenu;
     public float Aggressivity;                              // Bot Difficulty (0 = easy | 1 = normal | 2 = hard)
     public float BotMoveSpeed;                              // Bot movement speed
     public static float BotCurrentMass = 10f;               // Bot mass
     public static float BotCellSize = 0.2f;
     public float BotMaxDistance;                            // Max distance to see player/bot
     public float timeleft = 20f;
  
     public bool Neutral = true;                             // Neutra status
     public bool Hunting = false;                            // Hunting status
     public bool Escape = false;                             // Running away status
  
  
     private Transform myTransform;
  
     void Start()
     {
         myTransform = transform;
     }
  
     void Update()
     {
         Skin.transform.localScale = new Vector3(BotCellSize, BotCellSize, BotCellSize);
  
         // Neutral //
  
         if (Neutral == true)
         {
             Debug.Log(Name + " is neutral");
             Escape = false;
             Hunting = false;
  
             Vector3 dir = GameObject.FindGameObjectWithTag("food").transform.position - myTransform.position;
             dir.z = 0.0f;
  
             myTransform.position += (GameObject.FindGameObjectWithTag("food").transform.position - myTransform.position).normalized * BotMoveSpeed * Time.deltaTime;
         }
  
         // Hunting //
  
         if (BotCurrentMass > cball.CurrentMass)
         {
             Debug.Log(Name + " is hunting");
             Escape = false;
             Hunting = true;
             Neutral = false;
  
             Vector3 dir = GameObject.FindGameObjectWithTag("Player").transform.position - myTransform.position;
             dir.z = 0.0f;
  
             Vector3 dir2 = GameObject.FindGameObjectWithTag("bot").transform.position - myTransform.position;
             dir2.z = 0.0f;
  
             myTransform.position += (GameObject.FindGameObjectWithTag("Player").transform.position - myTransform.position).normalized * BotMoveSpeed * Time.deltaTime;
  
             myTransform.position += (GameObject.FindGameObjectWithTag("bot").transform.position - myTransform.position).normalized * BotMoveSpeed * Time.deltaTime;
         }
  
         // Escape //
  
         if (cball.CurrentMass > BotCurrentMass)
         {
             Debug.Log(Name + " is running");
             Neutral = false;
             Escape = true;
             Hunting = false;
  
             Vector3 moveDir = transform.position - GameObject.FindGameObjectWithTag("Player").transform.position;
  
             Vector3 moveDir2 = transform.position - GameObject.FindGameObjectWithTag("bot").transform.position;
  
             transform.Translate(moveDir.normalized * BotMoveSpeed * Time.deltaTime);
  
             transform.Translate(moveDir2.normalized * BotMoveSpeed * Time.deltaTime);
         }
  
     }
  
     void OnTriggerEnter2D(Collider2D food)
     {
         if (food.gameObject.tag == "Player")
         {
             if (cball.CurrentMass < BotCurrentMass)
             {
                 BotCellSize += cball.CellScale;
                 BotCurrentMass += cball.CurrentMass;
                 dmenu.SetActive(true);
                 Neutral = true;
                 Destroy(food.gameObject);
             }      
         }
         if (food.gameObject.tag == "food")
         {
             BotCellSize += 0.01f;
             BotCurrentMass += 1f;
             Destroy(food.gameObject);
             Neutral = true;
         }
  
         if (food.gameObject.tag == "bot")
         {
             BotCellSize += BotCellSize;
             BotCurrentMass += BotCurrentMass;
             Destroy(food.gameObject);
         }
     }
  
     void OnGUI()
     {
         GUI.Label(new Rect(10, 85, 100, 50000), "tm: " + timeleft);
     }
 }
 

Thanks.

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 BackslashOllie · Feb 01, 2016 at 03:50 PM 0
Share

Am I right in saying this script is attached to each 'bot' object? If so then the reason they are disappearing is because both bots are destroying each other when they collide with each other.

avatar image KINDOK BackslashOllie · Feb 01, 2016 at 05:08 PM 0
Share

Yeah. Each bot has this script. How do I make it so that the smallest disappear?

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

85 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need help with AI,Force not working 0 Answers

Enemy MoveTowards getting stuck on obstacles 1 Answer

Is it possible to add an Input Counter during Splash Screens? 0 Answers

I want my code to tell the AI to move from one log then back to the fire chamber and then to the other log but it just moves to the first log and doesnt do anything did i code something wrong? 2 Answers

Need AI to run to last known position it won't do that... 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