0

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;
    }
}
Samer
  • 1
  • 1
  • 3
    It is hard to see what the problem you are encountering in this code. Can you be specific on what result you are getting which is unexpected? – Clayton Wilkinson Jul 22 '15 at 23:37
  • @ClaytonWilkinson It wasn't clear to me until I followed the first link and watched the YouTube video through. Samer, perhaps you should show the tutorial code that uses mouse over event to set an `Enemy` as the `Player.opponent`, to better clarify what it is you *don't* want to do versus what you are trying to accomplish (`Player.Attack` = Finds closest enemy and attacks). – OhBeWise Jul 23 '15 at 14:42
  • Playmode will not start with the error: Assets/Player.cs(28,85): error CS1026: Unexpected symbol `;' expecting `)' – Samer Jul 23 '15 at 14:48
  • I updated OP codes, i get three errors: 1: Assets/Player.cs(23,25): error CS1525: Unexpected symbol `(', expecting `identifier'¨ 2: Assets/Player.cs(30,93): error CS1026: Unexpected symbol `;', expecting `)' 3: Assets/Player.cs(36,17): error CS8025: Parsing error – Samer Jul 23 '15 at 15:40
  • @Samer Those errors look like compile errors. 1 is telling you that in your Update function your line `Player. ();` is invalid. It wants you to call a function on the player class after the period. 2 is telling you you are missing a right parenthesis on the `if(Vector3.Distance...` line (you also have a semicolon there which is probably wrong). 3 is telling you that you are missing some right curly braces and that the compiler just can't figure out what is going on. Are the typos the problems you are having or is it a problem when you run it? – Becuzz Jul 23 '15 at 15:46
  • How can i set Enemy to know its a Player.opponent too? Or is that given already? And can i simply remove the mouseover script like it is? This is very interesting and fun but i sure have a long way to go :p – Samer Jul 23 '15 at 15:56

0 Answers0