I'm very new to the programming / scripting scene and I'm following a tutorial for making a basic controller and combat system (Birds view RPG). Thing is the tutorial will be making a "click to attack" (targeting) when me myself want to make a "attack closest enemy" type of combat.
What I have managed to do so far is having 3 opponents but I'm only able to attack 1 of them, thats where the instructor starts to add a targeting script which I wanna avoid. I tried finding someone else with a somewhat similar problem on answers.unity3d.com and tried to incorporate the code into my own project. The result seems to be slightly lacking and need help figuring out what it might be!
I did see many similar problems and tried to copy/paste code but there always seem to be minor issues I'm not familiar solving. (For example getting Tags to work?)
///Player code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public string name;
public int health;
public int damage;
public float range;
public Transform opponent;
void Start ()
{
}
void Update ()
{
Player. ();
}
void Player.Attack()
{
if(Input.GetKeyUp (KeyCode.Space))
{
if(Vector3.Distance(opponent.transform.position, transform.position);
{
if(opponent != null && Vector3.Distance (opponent.position, Transform.position) < Range)
{
opponent.GetComponent<Enemy>().GetHit(damage);
}
}
///Enemy code:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour
{
public string name = "Monster";
public int health;
public int damage;
void Start ()
{
}
void Update ()
{
}
public void GetHit(int playerDamage)
{
health = health - playerDamage;
}
void OnMouseOver()
{
Player.opponent = Transform;
}
}