• 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 crocodile5 · Mar 29, 2012 at 07:34 PM · guiguitextureobjectlabel

Problem about targetting multiple enemies

Currently I am using the ObjectLabel script to put a targetting reticle over targets in my space simulation game. But it works for just 1 target. I want the targetting reticle appear over all targets around the player.

So I created sphere with a collision trigger and added this script:

private var targetList : Array = Array();

private var targetPosition : Vector3;

var targetReticle : GameObject;

function Start () {

}

function Update () {

}

function OnTriggerEnter (other : Collider) {
targetPosition = other.gameObject.transform.position;

 targetReticle.SendMessage("AddItem",targetPosition);
 
     //Sends the target's position to the TargetReticle GUITexture object

}

Then I modified the ObjectLabel script as follows:

var target : Transform; // Object that this label should follow

var offset = Vector3.up; // Units in world space to offset; 1 unit above object by default

var clampToScreen = false; // If true, label will be visible even if object is off screen

var clampBorderSize = .05; // How much viewport space to leave at the borders when a label is being clamped

var useMainCamera = true; // Use the camera tagged MainCamera

var cameraToUse : Camera; // Only use this if useMainCamera is false

private var cam : Camera;

private var thisTransform : Transform;

private var camTransform : Transform;

//CUSTOM VARIABLES

private var targetList : Array = Array(); //List of Targets are stored here

var arrayLenght : int; //Lenght of the target array

private var i : int = 0;

function Start () {

 thisTransform = transform;
 
 if (useMainCamera)
 
     cam = Camera.main;
 
 else
 
     cam = cameraToUse;
 
 camTransform = cam.transform;
 

}

function Update () { var arrayLenght = targetList.length;

 for(i = 0; i <= arrayLenght; i++)
 
 {
     Debug.Log("Value of i:"+i);
 
     Debug.Log("Position of the target:"+targetList[i]);
 
     Debug.Log("Array lenght:"+arrayLenght);
 
     if (clampToScreen) {
 
         var relativePosition = camTransform.InverseTransformPoint(targetList[i]);
 
         relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
 
         thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
 
         thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
                                          Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
                                          thisTransform.position.z);
     }
 
     else {
 
         thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
 
     }
 }

}

//CUSTOM FUNCTIONS HERE

function AddItem(item : Vector3) {

targetList.Add(item);

}

@script RequireComponent(GUIText)

The code actually does get all the targets in the dedection sphere BUT it only puts the reticle on the last object in the array. How do I change the code to keep the reticle on till the objects get destroyed?

Thanks.

Comment
Add comment · Show 2
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 Owen-Reynolds · Mar 29, 2012 at 07:57 PM 1
Share

Is this script on your one target reticule?

If so, then it can only be on one place at the same time, which is the last place you assigned it. If you want markers on four enemies, you need to have four gameObject target reticules.

avatar image crocodile5 · Mar 30, 2012 at 01:18 AM 0
Share

Yes that worked thanks :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sirex · Mar 29, 2012 at 10:36 PM

Isn't it becouse you are actually only moving one sphere around and in the end it is on the last one? "So I created sphere with a collision trigger and added this script:" If you want more spheres you have to instantied them in the loop. Make this script more of a controller.

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 Sirex · Mar 30, 2012 at 07:25 AM 0
Share

Sorry didn't see the above comments, was tired :p

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

Toggle GuiTexture 1 Answer

blood splatter 3 Answers

need help croping texture.... something wrong with my guigroup. 0 Answers

Get GUITexture to follow ScreenPointToRay ray cast 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