• 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 /
  • Help Room /
This question was closed Apr 06, 2017 at 02:09 PM by Kerno for the following reason:

Solved problem by myself

avatar image
0
Question by Kerno · Mar 30, 2017 at 03:31 PM · scripting problemvector3melee attack

creating 3d melee cone splash attack

Hello! Trying to learn the ropes around Unity3d, so please dont judge me to hard. I've been struggling around with one problem that I couldnt find a good solution for. I need to make a melee cone splash attack that player's character is performing on left click. For that I need to find all the enemies in front of the player in a particular range and reduce their health. First I'm collecting enemies into an array with Physics.OverlapSphere and then I check that only those in front of the player are hit with Vector3.dot. So first part about getting all the enemies in range around the player is going smoothly, but once I get to the part where I check their location with Vector3.dot, I start to get pretty odd behaviour and weird angles. Where I would expect an angle of 60 degrees or even less, I get angles 120 degrees and even more and therefore in situations where hit is supposed to be scored - it doesnt happen, and when there is no way it could happen (character is standing with it's back to the enemy) - it happens. Here's the code of player's attack script:

  using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class PlayerAttack : MonoBehaviour {
          
         public int damagePerSwing = 20;
         public float timeBetweenSwings = 0.15f;
         public float SwingRange;
     
     
         float timer;
         Ray swingRay;
         RaycastHit swingHit;
         int shootableMask;
         int layertest = 1 << 9;
     
         void Awake(){
         
             shootableMask = LayerMask.GetMask ("Hittable");
         }
 
     void Update() {
             timer += Time.deltaTime;
             if (Input.GetButton ("Fire1") && timer >= timeBetweenSwings) {
             
                 Swing ();
             }
         }
 
         void Swing (){
             timer = 0f;
     
             swingRay.origin = transform.position;
             swingRay.direction = transform.up;
 //that's where I check who's nearby
                Collider[] Enemy_col = Physics.OverlapSphere(transform.position, SwingRange, layertest );
               int i = 0;
             float inRange = 0f;
             while (i < Enemy_col.Length) {
 //there I get vector3.dot from player's front vector and player-to-enemy vector (which I get by substracting as follows)
                  inRange = Vector3.Dot (swingRay.GetPoint(SwingRange).normalized, Enemy_col[i].transform.position.normalized - transform.position.normalized);
                    //that's where I check that I can hit those enemies
                 if (inRange > 0f && inRange < 1f) {
                      //and there's an actual hit happens  
                     swingRay.direction = Enemy_col[i].transform.position;
                     Physics.Raycast (swingRay, out swingHit, SwingRange, shootableMask);
                     EnemyHealth enemyHealth = Enemy_col[i].GetComponent<EnemyHealth> ();
                     if (enemyHealth != null) {
                         enemyHealth.TakeDamage (damagePerSwing, swingHit.point);
                     }
                     swordLine.SetPosition (1, swingHit.point);
                 }
                 i++;
             }
     
         }
         
     }
 

I have a feeling that if I dont find any solution to this I'll just have to redesign whole script and prehab for an additional gameobject "Cone" with collider triggers and etc and my guts tell me it's not that elegant to go this way.

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
1

Answer by Kerno · Apr 06, 2017 at 02:08 PM

had to use transformdirecton to solve my problem.

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 allcoder · May 14, 2017 at 04:25 AM 0
Share

Yeah i did this solution

     bool frontAttack(Transform target)
     {
         Vector3 forward = transform.TransformDirection(Vector3.forward);
         Vector3 toTarget = target.position - transform.position;
         float angle = Vector3.Dot(forward, toTarget);
         
         if(angle > 0.5f && angle < 1.5f)
         {
             return true;
         }

         return false;
     }

Dot returns 1 if they point in exactly the same direction, -1 if they point in completely opposite directions and zero if the vectors are perpendicular. So angle > 0.5f && angle < 1.5f is a good angle

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

how to provide axes input to fpscontroller according to maincamera y rotation ? 0 Answers

InverseLerp for vector3 1 Answer

Searching a function to get the rotation of one Vector3 in relation to another 1 Answer

New Vector2 won't take effect when called from another script in the same GameObject. 0 Answers

+= operator on transform.localPosition results in random values 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges