Trouble killing closest enemy
Hi! I'm currently working on my first game, a quick-time game where enemies spawn with letters on top of them which the player presses the corresponding key to kill the enemy. I want it so the player can only kill the closest enemy to it, which I have set up to find. The problem is that the "killKey" doesn't lineup to the closest enemy, but rather the enemy that recently spawned. I'm fairly certain that the find closest enemy part of my script does indeed work (as shown by the debug.drawline). Heres my script: Player:
public class Player : MonoBehaviour
{
public string keyPressed;
public int penaltyTimer;
public GameObject target;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
var allKeys = System.Enum.GetValues(typeof(KeyCode)).Cast<KeyCode>();
FindClosestEnemy();
if (Input.GetKeyDown("w") || Input.GetKeyDown("a") || Input.GetKeyDown("s") || Input.GetKeyDown("d"))
{
foreach (var key in allKeys)
{
if (Input.GetKeyDown(key))//finds what key the player pressed
{
Debug.Log(key + " was pressed.");
keyPressed = key.ToString();
}
}
SwordStrike();
}
}
void FindClosestEnemy()
{
float distanceToClosestEnemy = Mathf.Infinity;
GameObject closestEnemy = null;
GameObject[] Allenemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach (GameObject currentEnemy in Allenemies)
{
float distanceToEnemy = (currentEnemy.transform.position - this.transform.position).sqrMagnitude;
if (distanceToEnemy < distanceToClosestEnemy)
{
distanceToClosestEnemy = distanceToEnemy;
closestEnemy = currentEnemy;
}
target = closestEnemy;
Debug.DrawLine(this.transform.position, target.transform.position);
}
}
void SwordStrike()
{
if (target.GetComponent<EnemyMovement>().killKey == keyPressed)
{
Object.Destroy(target.gameObject);
}
}
}
Enemy:
public class EnemyMovement : MonoBehaviour
{
public float speed;
private Transform objective;
public GameObject Letter;
public string killKey;
void Start()
{
objective = GameObject.FindGameObjectWithTag("Player").transform;
GetRandomLetter();
}
// Update is called once per frame
void Update()
{
//move towards objective
transform.position = Vector2.MoveTowards(transform.position, objective.position, speed * Time.deltaTime);
if (Vector2.Distance(transform.position, objective.position) < .5)//if enemy hits player
{
Destroy(this.gameObject);
}
}
void GetRandomLetter()
{
string[] wLetter = { "W", "A", "S", "D" };
int randomLetter;
randomLetter = UnityEngine.Random.Range(0, wLetter.Length);
killKey = wLetter[randomLetter];
if (Letter)
{
GameObject Text = Instantiate(Letter, transform.position, Quaternion.identity);
Text.transform.parent = gameObject.transform;//sets as parent
}
Letter.GetComponent<TextMesh>().text = killKey;
}
}
Thanks in advance!
Answer by ThatGuy52699 · Sep 18, 2021 at 02:31 AM
I found out the issue. I thought I checked it but the letter on the enemy was different from the variable Key on the enemy. it works now though. All I did was move up the Letter.GetComponent line above the if statement.
void GetRandomLetter()
{
string[] wLetter = { "W", "A", "S", "D" };
int randomLetter = UnityEngine.Random.Range(0, wLetter.Length);
Key = wLetter[randomLetter];
Letter.GetComponent<TextMesh>().text = Key;
if (Key != null)
{
if (Letter)
{
GameObject Text = Instantiate(Letter, transform.position, Quaternion.identity);
Text.transform.parent = gameObject.transform;//sets as parent
}
}