Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 FranktheGame · Mar 08, 2020 at 10:33 PM · raycastdamageinterfacefield of view

Melee damage with OverlapSphere to detect closest enemy in field of view of player

I am trying to make a melee script that does damage to the closest enemy in the player's field of view. I am using an OverlapSphere to detect potential colliders, iterating through an array of them, filtering out any not in the field of view, identifying the closest one, then a raycast to make sure no obstruction. I have been able to do damage so I know it's working somewhat, but for the most part it wont damage even when right in front of the target. It seems I am not getting the right angles for the FOV check as it always registers 90 degrees when my crosshair is centered on the target when I assume should be 0. Am I missing something there or somewhere else or probably multiple places?

 public class Sword : WeaponBase
 {
     public float attackDamage = 40f;
 
     public float fovRange = 2f;
     private Collider closestTarget;
     private Vector3 dirToTarget;
     float distance;
 
     private int layerMask = 1 << 8; //select layer 8
     [Range(0, 360)] public float fovAngle = 30.0f;
 
 
     void Start()
     {
         attackCoolDown = 0.55f;
         hand = "Right";
         closestTarget = null;
         distance = Mathf.Infinity;
     }
 
     public override void Attack()
     {
 
         Collider[] hitColliders = Physics.OverlapSphere(transform.position, fovRange, layerMask); //get all the objects that could potentially be in field of view.
 
         for (int i = 0; i < hitColliders.Length; i++) //make a loop to check whats there
         {
             dirToTarget = hitColliders[i].transform.position - transform.position;  // get target direction then check against field of view
             float angle = Vector3.Angle(dirToTarget, transform.forward); 
             Debug.Log("Target Angle = " + angle);
 
             if (angle < fovAngle)
             {
                 float curDistance = dirToTarget.sqrMagnitude;
                 if (curDistance < distance)
                 {
                     closestTarget = hitColliders[i];
                     distance = curDistance;
                     Debug.Log("Closest Target = " + closestTarget.gameObject.name);
                 }
             }
             else
             {
                 return;
             }
         }
         if (closestTarget != null)
         {
             // check to make sure nothing is obstructing 
             RaycastHit hit;
             Physics.Raycast(fpsCam.transform.position, dirToTarget, out hit, layerMask);
             {
                 IDamageable damageable = hit.transform.GetComponent<IDamageable>();  // already checked for this above but not sure which one to get rid of yet
                 if (damageable != null)
                 {
                     Debug.Log("You hit " + damageable);
                     damageable.ApplyDamage(attackDamage);
                     closestTarget = null;
                 }
             }
         }
         else
         {
             return;
         }
     }
 }
 
Comment
Add comment · Show 6
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 joemane22 · Mar 08, 2020 at 11:13 PM 1
Share

Does the angles still change linearly? If not how do they change? What I mean is as you move where the player is looking relative to the target how does the calculated angle change? I am not seeing the problem yet so trying to get more information.

avatar image FranktheGame joemane22 · Mar 08, 2020 at 11:23 PM 0
Share

Yeah it seems pretty linear, though to the left would be less than 90 so subtracting 90 would get negative number, which is fine and what I would expect anyway, but figured the wrong angle was indicating my method wasn't right. I just tried subtracting 90 from the angle and it gives the numbers I was expecting, though still not registering hits for damage for some reason. It seems to do damage only if I have 2 or more targets in proximity.

avatar image joemane22 FranktheGame · Mar 08, 2020 at 11:54 PM 1
Share

$$anonymous$$ake sure you are resetting the distance variable each time attack is called at the start of the function otherwise it will not select the right one. See of that helps. If you are still having trouble step through the code where you should get a hit and figure out where it is rejecting the hit.

Show more comments
avatar image joemane22 · Mar 08, 2020 at 11:15 PM 1
Share

If it changes linearly just subtract 90 and be done with it lol. Even flip the signs or what ever you need to do to make it align with the expected values.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_ek98vnTRplGj8Q · Mar 09, 2020 at 03:45 PM

I see two potential issues here: I see that this logic is inside your sword class. Does this mean that it is attached to a sword object, not the player object? It seems like if you are checking FOV you want to check fov from player position and rotation, not sword position and rotation. Also make sure that the angle between the target and the player is correct vertically, for example if the player origin is at the player's head and the target origin is at its feet the even when looking right at the target Vector3.Angle() will return the vertical angle, making it seem like the object is closer to the edge of FOV than it is

Comment
Add comment · Show 5 · 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 FranktheGame · Mar 09, 2020 at 04:04 PM 0
Share

Yes, it's a sword that can be picked up and dropped. I am surprised I didn't think about that but makes sense. It also explains why there were times it was doing damage to the player. Do you have a suggestion for finding the right angle? Checking against transform.up or something?

avatar image unity_ek98vnTRplGj8Q FranktheGame · Mar 09, 2020 at 04:45 PM 1
Share

In the sword class I would just hold a reference to the player transform and check from there. As far as getting the right angle it really depends on exactly how your game is set up and what you want the behavior to be. For example, the quick and easy way would just be to set the y component of the direction between the target and the player to zero so that you are only checking the horizontal angle, as well as set the y component of the player's transform.forward to zero (this way you only compare angles on the XZ plane). However this will cause enemies to still be hit if you are looking way up or way down, (if you are using a first-person setup for example) which may or may not be something you want. It could also cause a few other bugs, but if you are doing a 3rd person game this could work quite well.


The code you have should work alright though as long as you just use the player transform. I only mentioned this because I could forsee an issue where you were getting the direction from player's eye level to enemy's feet which could cause some issues. Just make sure you think about how you want FOV to work when, for example, the enemy is above the player or the player is looking up or down.

avatar image FranktheGame unity_ek98vnTRplGj8Q · Mar 09, 2020 at 06:08 PM 0
Share

Yeah I this is for 1st person so I'll have to see if this is the best option for what I'm going for. Either way, I'm learning a lot so thanks a lot for the tips! I'll play around with it all and see if I can get it all working.

Show more comments

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

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

Related Questions

Health System Raycast Bullets 2 Answers

Plz help I have a problem in my damage popup 0 Answers

How to apply damage to raycast hitten object's Networkview? 2 Answers

AI detection script problem 0 Answers

I'm trying to get a Raycast Laser weapon to tell whatever it hits to do damage, and its not working. im kindof a noob... 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