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
1
Question by convictcartel · Dec 23, 2013 at 05:18 PM · collisionsphererpgmeleecombat

Melee Combat - Collision Detection

Im stuck.. I have a player and all my enemies tagged with "enemy"! Now Ive been searching, trying collision scripting but all ive managed to do was delete the game object on collison, I want to be able to press F and make the Attack function work! Detect if Im near an enemy and if there are any near me in my collision, call the function.. Seems easy right? Im new to this bout 5 days ago so any help please!! C# Script -

 using UnityEngine;
 using System.Collections;
 
 
 public class PlayerAttack : MonoBehaviour {
     public GameObject target;
     public float attackTimer;
     public float coolDown;
 
     // Use this for initialization
     void Start () {
         attackTimer = 0;
         coolDown = 2.0f;
     }
     
     // Update is called once per frame
     void Update () {
 
         if(attackTimer > 0)
             attackTimer -= Time.deltaTime;
             
         if(attackTimer < 0)
             attackTimer = 0;
             
     //attack press F key
         if (Input.GetKey (KeyCode.F)) {
         if(attackTimer == 0) {
             Attack();
             attackTimer = coolDown;
             }
         }
     
         
 
     }
 
 
     // CHANGE TO HIT NEAREST ENEMY?? IDK HOW MAN!!!
 
     private void Attack() {
                         AttackandHP eh = (AttackandHP)target.GetComponent ("AttackandHP");
                         eh.AdjustCurrentHealth (-2);
                 }
 
     
 }
 
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 Spinnernicholas · Dec 23, 2013 at 06:01 PM 0
Share

Your not actually doing any collision detection.

Is the game using 2D colliders or 3D colliders?

avatar image william9518 · Dec 23, 2013 at 06:08 PM 0
Share

Go on god damn google and search "find closest gameObject with tag" and check if that gameObject is close enough and if you are hitting the F button and damage that object.

2 Replies

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

Answer by fafase · Dec 23, 2013 at 09:32 PM

One problem will rise using the closest enemy, how do you hit to enemy with one melee if they are next to each other? Even though they are both in range, only the closest one is taken care of, which is not what the player expect to happen.

So what you could do is add a sphere collider to your player, set it to trigger, this sphere would have the radius matching the melee range. Then there you go:

 List<GameObject>nearEnemy = new List<GameObject>();
 void OnTriggerEnter(Collider col)
 {
    if(col.gameObject.tag == "Enemy")nearEnemy.Add(col.gameObject);
 }
 void OnTriggerExit(Collider col)
 {
    if(col.gameObject.tag == "Enemy")nearEnemy.Remove(col.gameObject);
 }


Now you can add this below as an animation event http://docs.unity3d.com/Documentation/Components/animeditor-AnimationEvents.html so that it happens at an appropriate time during the melee animation.

 void Melee()
 {
    Vector3 pos = transform.position;
    for(int i = 0; i < nearEnemy.Count;i++)
    {
       Vector3 vec = nearEnemy[i].transform.position;
       Vector3 direction = vec - pos;
       if(Vector3.Dot(direction, transform.forward)<0.7))
       {
           nearEnemy[i].GetComponent <AttackandHP>().ApplyDamage(damage);
       }
    }
 }

All it does is going through any enemy that is within the sphere and check with dot product if the guy is on the front at the time of call.

Comment
Add comment · Show 4 · 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 convictcartel · Dec 23, 2013 at 10:24 PM 0
Share

Where do I place these codes and are they javascript?

avatar image fafase · Dec 25, 2013 at 03:28 PM 0
Share

They go on the player and it is C#. Pretty simple to convert though.

avatar image SureSight · Feb 17, 2016 at 10:10 AM 0
Share

I've used a solution similar to this one, but I find that it fails to calculate hits for GameObjects that are wider than an average human. When attacking the broad side of a gate, door, barricade,etc. the Dot product fails to calculate a hit because the player is attacking the side/corner ins$$anonymous$$d of facing the body position/root position (target's center).

The only way I can think to solve this is to get the Dot product and then RayCast toward the target as a redundant measure, but this becomes very expensive at runtime.

Does any have a better idea of a more reliable means of calculating a hit for normal sized and large sized targets?

avatar image metamorphist SureSight · Apr 06, 2016 at 05:45 AM 0
Share

I think you can also calculate the enemy's transform and its relation to the player's position

In world space, playertransformposition - enemytransformposition will acquire you the vector's position that you can use in localspace in relation to the player's forward direction vector. You can then compare this two vector's angle in localspace, and as long as it is within a certain angle, you can then register the attack as hit or not, hit forward or back, etcs.

avatar image
0

Answer by Divine · Dec 23, 2013 at 08:02 PM

here

The second example should work out of the box. Just replace

 AttackandHP eh = (AttackandHP)target.GetComponent ("AttackandHP"); 

with

 AttackandHP eh = FindClosestEnemy.GetComponent<AttackandHP>();

Always use the generic one. Always.

Comment
Add comment · Show 1 · 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 convictcartel · Dec 23, 2013 at 08:53 PM 0
Share

Two errors. I added the second code and replaced the attack one. BTW Thank you very much for taking the time to help, appreciate it man!

error CS0119: Expression denotes a method group', where a variable', value' or type' was expected

error CS0165: Use of unassigned local variable `closest'

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

melee combat 0 Answers

How can i detect collision in melee combat? 2 Answers

Best way to detect sword hit 0 Answers

IK for Precise Parrying Animations - Sword Combat System 1 Answer

Full Performance On Melee Combat 0 Answers


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