Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by captainvera · Apr 11, 2013 at 05:29 PM · 2djavascriptmeleecombat

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!!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image captainvera · Apr 12, 2013 at 11:35 PM 0
Share

no one? :/

avatar image captainvera · Apr 13, 2013 at 09:23 PM 0
Share

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!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Vonni · Apr 13, 2013 at 12:15 AM

  1. 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.

  2. Most important. You have if(HIT && hit.tag == ...whatever..){ What is hit supposed to be here?

  3. 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);
  
             }
  
        }
       }
     }
  
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image captainvera · Apr 13, 2013 at 07:56 PM 0
Share

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!

avatar image captainvera · Apr 14, 2013 at 05:53 PM 0
Share

Solved i was in full retard mode yesterday, i had made the tag but hadn't put on the enemy --'

avatar image Vonni · Apr 15, 2013 at 12:30 AM 0
Share

Goodyyy :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges