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 Alanimator · Oct 11, 2013 at 05:50 AM · c#distancecrosshairlockon

crosshairs lock-on trouble . locking on to closest enemy but wont change target

so i created a method by which i can draw a lockon on enemies that are near by . Unfortunately once it locks on to target it maintains that target as the only target. here is the entire code :)

only three (3) main functions.

you attach the script to your player gameobject.

can you help me get this thing to work correctly >.< thanks in advance . and sorry for the messy code ^^

 using UnityEngine;
 using System.Collections;
 
 public class Lockon : MonoBehaviour {
     public Texture2D Crosshair;
 
     private Camera myCamera;
     
     private Transform myTransform;
     
     private Vector3 worldPosition  = new Vector3();
     
     private Vector3 screenPosition  = new Vector3();
     
     private Vector3 cameraRelativePosition = new Vector3();
     
     private float minimumDistanceZ = 30.0f;
     
     private float adjustment = 1;
     private GUIStyle LockonStyle = new GUIStyle();
     private float dist2;
     public Transform enemyship;
     float distance = Mathf.Infinity;
     public GameObject closest;
     
     
     
     
     
     
     // find the closest enemy ----------------start--------------
     GameObject FindClosestEnemy() {
         
         GameObject[] gos;
         gos = GameObject.FindGameObjectsWithTag("lazerhit");   
         Vector3 position = transform.position;
         foreach (GameObject go in gos) {
             Vector3 diff = go.transform.position - position;
             float curDistance = diff.sqrMagnitude;
             if (curDistance < distance) {
                  closest = go;
                 distance = curDistance;
             }
         }
         return closest;
             
         
     }
     
     //-----------------------End-----------------------------------
     
     
     
     
 
     // Update is called once per frame
     void Update () {
         // set enemyship to the closest gameovject with the tag "lazerhit"
         enemyship =FindClosestEnemy().transform;
         
         // GetType distance from player to enemy
      dist2 = Vector3.Distance(enemyship.position, gameObject.transform.position);
         
         //-------------------------------------------
             myTransform = enemyship;
             
             myCamera = Camera.main;
 
                 //Capture whether the player is in front or behind the camera.
         
         cameraRelativePosition = myCamera.transform.InverseTransformPoint(enemyship.transform.position);
         
         
         LockonStyle.normal.background = Crosshair;
         
 
         FindClosestEnemy();
 
     }
     
     
     void OnGUI (){
         if(cameraRelativePosition.z > minimumDistanceZ && dist2 < 400.0f)
         {
         
         worldPosition = new Vector3(myTransform.position.x, myTransform.position.y + adjustment,
                                         myTransform.position.z);    
             
             screenPosition = myCamera.WorldToScreenPoint(worldPosition);
             
             GUI.Label(new Rect(screenPosition.x - 40 / 2,Screen.height - screenPosition.y - 20,40, 40),"",LockonStyle);
             
             }
 
     
 
         
     }
     
     
     
     
     
     
     
     
     
     
     
     
 }
 
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
2
Best Answer

Answer by robertbu · Oct 11, 2013 at 06:03 AM

I see two problems, one of which may account for your issue. On line 40 you have:

 if (curDistance < distance)

...but 'distance' is never reinitialized to 'Mathf.Inifinity. This means that as the closest moves further away from the player, this code will not find closer enemies unless they happen to be closer than 'distance'.

You need to add this line to the top of FindClosestEnemy():

 distance = Mathf.Inifinity;

Minor issue is line 77. You never assign the return value of this FindClosestEnemy() call, so 'enemyship' is not being set here (assuming that was your intent).

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 Alanimator · Oct 11, 2013 at 06:27 AM 0
Share

line 77 , oh yes that . that really should have been removed long ago . I overlooked it . hey thanks :) it works now . I had noticed that there was something not being updated but i never actually looked at distance = $$anonymous$$athf.Infinity; . thanks again

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

15 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

What's the best way to find the smallest Vector3.Distance of an array of enemies? 2 Answers

Classic Game Distance masking darkness/fog help 1 Answer

How to make the Crosshair follow delayed 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