[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
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.
Yeah. Each bot has this script. How do I make it so that the smallest disappear?