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 Shmks · Aug 22, 2014 at 04:18 PM · raycastfov

Using FOV to target multiple enemies

Hey folks, I'm having some issues getting my targeting to work here. The desired behavior is for the player to be able to target several enemies (controlled by int maxLocks) within the FOV angle (controlled by float reticuleAngle). The array availableTargets holds the targetable enemies, and the loop goes through that to see if we have any within reticuleAngle.

How can I get the angle check to work on both X and Y axies?

     void EyeTarget ()
     {
         //This line builds an array of all the Enemies in the scene so that the multi-targeting reticule
         // can calculate the angle from the camera to the target.
         availableTargets =  GameObject.FindGameObjectsWithTag ("Enemy");
 
         RaycastHit hit;
 
         for (int i = 0; i < availableTargets.Length; i++) 
         {
             //Check if each target is in the reticule
             if (Vector3.Angle(transform.forward, availableTargets[i].transform.position - transform.position) < reticuleAngle)
             {
                 Vector3 rayDirection = availableTargets[i].transform.position - transform.position;
                 Ray ray = new Ray (transform.position, rayDirection);
 
                 if (Physics.Raycast (ray, out hit, attackRange)) 
                 {
                     
                     //Check each enemy available targets for a hit
                     if (hit.collider.gameObject == availableTargets[i]) 
                     {
                         
                         Debug.DrawLine (ray.origin, hit.point);
                         
                         //start the lock timer
                         targetingTime += Time.deltaTime * 1;
                         
                         //if the current target changes reset the timer
                         if (hit.collider.gameObject != availableTargets[i])
                         {
                             ResetTimer ();
                             Debug.Log ("left Switched Targets");
                             
                         }
                         
                         //if the target is under the cursor for the required time lock on.
                         if (targetingTime >= lockTime && leftNumLocks < maxLocks) 
                         {
                             lockedTargets[leftNumLocks] = availableTargets[i];
                             leftNumLocks++;
                             targetingTime = 0f;
                         }
     
                         // For multi targeting, check to see if anything in the list is the same
                         // as the current target, if it is, reset the timer to prevent lock.
         
                         for (int cnt = 0 ; cnt < lockedTargets.Length; cnt++)
                         {
                             if ( lockedTargets[cnt] == currentTarget )
                             {
                                 targetingTime = 0f;
                             }
                         }
         
                         currentTarget = availableTargets[i];
                         currentTargetName = hit.collider.name;
                     }
                 }
             }
             else 
             {
                 Debug.Log ("OUTSIDE RET" + availableTargets[i]);
                 ResetTimer ();
                 currentTarget = null;
             }
         }
     }



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

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

Answer by tanoshimi · Aug 22, 2014 at 04:21 PM

Your Ray() constructor looks wrong. The syntax should be new Ray(origin, direction) (http://docs.unity3d.com/ScriptReference/Ray-ctor.html).

You're passing:

 Ray ray = new Ray (rayDirection, availableTargets[i].transform.position);


My guess is that you meant:

 Ray ray = new Ray (transform.position, rayDirection);
Comment
Add comment · Show 2 · 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 Shmks · Aug 22, 2014 at 06:25 PM 0
Share

Thanks, that totally worked, I've updated the code in the above post. I can now see that the rays are hitting multiple targets. However its clear now that there are more problems, first of which is that the angle check

 if (Vector3.Angle(transform.forward, availableTargets[i].transform.position - transform.position) < reticuleAngle)
 

is only operating on the X axis. How can I check the Y axis also to get a "circle" shaped targeting area ins$$anonymous$$d of a vertical "stripe"?

avatar image Shmks · Aug 22, 2014 at 06:45 PM 0
Share

Found it! I was using the player position.transform ins$$anonymous$$d of the camera. After I changed it I got the X and Y to work.

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

2 People are following this question.

avatar image avatar image

Related Questions

Help with FOV script 1 Answer

Check if player is in enemys fov? 1 Answer

AI Field of vision 1 Answer

Non uniform movement radius 1 Answer

Make Raycast ignore anything that "Isn't" my player(Solved) 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