• 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.

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

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

Ray/line casting for camera angle 1 Answer

Rotated Camera fix for Screen to World point overshoots 0 Answers

Angles from Directions 2 Answers

Find angle between two gameobjects? 1 Answer

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