Here is the army code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Army : MonoBehaviour
{
public int ArmySize = 100;
private Vector2 lastClickedPos;
private bool moving = false;
public float speed;
public SpriteRenderer sprite;
private GameObject Village;
private void Start()
{
Village = GameObject.FindGameObjectWithTag("Village");
}
private void Update()
{
if(Input.GetMouseButton(0))
{
sprite.enabled = true;
lastClickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
moving = true;
}
if(moving && (Vector2)transform.position != lastClickedPos)
{
float step = speed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, lastClickedPos, step);
}
else
{
moving = false;
}
if(ArmySize < 0)
{
ArmySize = 0;
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("Village"))
{
Debug.Log("Attack");
if (ArmySize <= Village.GetComponent<Village>().strength)
{
Debug.Log("Lost");
ArmySize -= 30 % Village.GetComponent<Village>().strength;
}
if (ArmySize > Village.GetComponent<Village>().strength)
{
Debug.Log("Won");
ArmySize += 15% Village.GetComponent<Village>().strength;
Destroy(other.gameObject);
}
}
}
}
and here is the village code:
using UnityEngine;
public class Village : MonoBehaviour
{
public int strength;
private int x;
private void Start()
{
StrengthValue();
strength = x;
Debug.Log(gameObject.name + " ---- " + strength);
transform.localScale = new Vector3(strength / 100, strength / 100, 1);
if(transform.localScale.x < 1 && transform.localScale.y < 1)
{
Debug.Log(gameObject.name + "Is Destroyed");
Destroy(gameObject);
}
}
private void StrengthValue()
{
x = Random.Range(10, 500);
}
}
the problem is that it sometimes doesn't destroy the village even though the army size is bigger than the village's strength. I also got an error but I can't seem to re-create it.