- Home /
 
Melee combat, sphereoverlap not working
Hi guys,
so, on my quest to learn unity i decided to make a 2d mini rpg.. And after doing some basic scripting and movement i decided i wanted to try making a basic melee system but it doesn't work past the Debug.Log("Step 1") even if i'm standing right next to a cube with a Enemy health Script with the "damageDeal" function in it. Why? what did i do wrong
Here it is:
 var dmg : int = 10;
 var Cooldown : float = 0.5;
 var Attacktimer : float = 0;
 var Range : float = 20;
 var dist : float;
 function Update () {
 Attacktimer += Time.deltaTime;
 
     if (Input.GetMouseButtonDown(0))
     {
     Debug.Log("Step 1");
         // animation.Play("Attack");
         Attacktimer = 0;
         
         var collisions : Collider[] = Physics.OverlapSphere(transform.position, Range);
         for (var hit : Collider in collisions) {
         
           
            if (hit && hit.tag == "Enemy"){ 
                 
             
             dist = Vector3.Distance(hit.transform.position, transform.position);
             
             Debug.Log(dist);
             Debug.Log("Step 2");
             if (dist <= Range)
             {
             
             hit.gameObject.SendMessage("damageDeal", dmg);
             
             }
     
         }
       }
     }
 
 }
 
               Sorry if this is something obvious, im new ;)
OH! and thanks in advance!!
It'd be awesome if an ad$$anonymous$$ could change the tittle to something like : "Send$$anonymous$$essage doubt" of something similar because the title doesn't make a good job at describing my current question as the sphereoverlap was working fine!
Answer by Vonni · Apr 13, 2013 at 12:15 AM
Do not use capital letters when starting variables, its confusing to read, they are used for functions and classes. As you had a Range variable, there is also a Range() function. I changed all of them for you.
Most important. You have if(HIT && hit.tag == ...whatever..){ What is hit supposed to be here?
Havent tested this tho, but think it will work now, if not let me know
var dmg : int = 10; var cooldown : float = 0.5; var attackTimer : float = 0.0; var range : float = 20.0; var dist : float;
function Update () { Attacktimer += Time.deltaTime;
     if (Input.GetKeyDown(KeyCode.Mouse0))
     {
     Debug.Log("Step 1");
        // animation.Play("Attack");
        attacktimer = 0;
  
       var collisions : Collider[] = Physics.OverlapSphere(transform.position, range);
       for (var hit : Collider in collisions) {
  
  
         if (hit.tag == "Enemy"){ 
  
  
             dist = Vector3.Distance(hit.transform.position, transform.position);
  
             Debug.Log(dist);
             Debug.Log("Step 2");
             if (dist <= range)
             {
  
             hit.gameObject.SendMessage("damageDeal", dmg);
  
             }
  
        }
       }
     }
  
 }
 
              1- Thanks for the tip! corrected it! 2- it wasn't due to the (HIT && hit.tag ==..) thing, i had the tag on my enemy as "enemy" ins$$anonymous$$d of "Enemy"... such a retarded mistake.. 3- It now reaches the Step 2 and even sends the message but i gives me a no receiver error even though i have the following script on my enemy:
 var currentHealth : int = 0;
 var maxHealth : int = 100;
 
 function Star() {
 
 currentHealth = maxHealth;
 }
 
 
 function Update () {
 
 if ( currentHealth <= 0)
     Destroy(gameObject); 
 }
 
 
 function DamageDeal (dmg : int)
 {
 
 currentHealth -= dmg; 
 
 }
 
                  note: yes i am aware that the script i pasted on the OP called the function with a small letter at the beginning but after your not on variable names i changed all functions to a capital letter, here and on the dmg script.
Thanks in advance!
Solved i was in full retard mode yesterday, i had made the tag but hadn't put on the enemy --'
Your answer
 
             Follow this Question
Related Questions
Brawl style melee combat? 0 Answers
character area attack problem 1 Answer
How to make it so enemies only move towards the player when the player is colliding with an object 1 Answer
Insert a semicolon at the end Error. (JavaScript) 1 Answer
How to make a function stop when all the Gameobjects from the array are in the Game? 1 Answer