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 Anymeese · Mar 24, 2014 at 05:34 PM · cameramathanglecurvelockon

Get the gameobject to the left/right of current target, in field of vision?

I'm trying to write the math to cycle between lockon targets. Basically the player is currently locked onto one target (looking at them, the target has an eye over its head etc etc etc). If the player hits left on rightHorizontalStick, it should switch to the next viable target to the left of the current one, and same for if they hit right. The problem is that rather than just being left/right on one axis, it's relative to the curve that is made up by the camera's vision. Depth does not matter at all, only left/right position along this curve

Here are two picture to better communicate what I mean (the black dots are other enemies):

http://i.imgur.com/vqA3a6S.png

http://i.imgur.com/7f0keyD.png

How could I get the next dot to the left/right of the blue dot?


Here is the code from my attempt. Idk if I just need to tweak the math or if there is an entirely different, much better approach, but just in case, I figured I should post what I did myself

 private void changeLockOnTarget(direction d)
 {
     float yRot = player.transform.eulerAngles.y;
     float distanceFromCurrLockOn = Mathf.Infinity;
     GameObject newTarget = null;
     GameObject[] allEnemies = GameObject.FindGameObjectsWithTag("Enemy");
     
     foreach(GameObject enemy in allEnemies)
     {
         //if not current target && enemy is in camera view
         if(enemy != lockOnTarget && GeometryUtility.TestPlanesAABB(GeometryUtility.CalculateFrustumPlanes(mainCam), enemy.collider.bounds))
         {
             float distance;
             if(d == direction.left)
             {
                 float xAmount = (lockOnTarget.transform.position.x - enemy.transform.position.x) * Mathf.Cos(yRot * Mathf.Deg2Rad);
                 float zAmount = Mathf.Abs(lockOnTarget.transform.position.z - enemy.transform.position.z) * Mathf.Sin(yRot * Mathf.Deg2Rad);
                 
                 distance = xAmount + zAmount;
             }
             else if(d == direction.right)
             {
                 float xAmount = (enemy.transform.position.x - lockOnTarget.transform.position.x) * Mathf.Cos(yRot * Mathf.Deg2Rad);
                 float zAmount = Mathf.Abs(enemy.transform.position.z - lockOnTarget.transform.position.z) * Mathf.Sin(yRot * Mathf.Deg2Rad);
                 
                 distance = xAmount + zAmount;
             }
             else distance = 0; //this will never happen
             
             if(distance < distanceFromCurrLockOn && distance > 0)
             {
                 Debug.Log("newTarget is now " + enemy.name);
                 distanceFromCurrLockOn = distance;
                 newTarget = enemy;
             }
         }
     } 
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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Instantiate cube on network 0 Answers

Given a position, how do I figure out Pixel's Per Unit? 1 Answer

Please help, bullet drop is wrong at certain angles ? 0 Answers

Transform.RotateAround(Vector3 axis, float angle) - what happens with only two arguments? 3 Answers

How to calculate the angle of a trajectory to hit the target 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