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 JoshMBeyer · Mar 24, 2014 at 10:09 AM · guitargeticontabtargetting

Can't Figure out how to go about creating this part.

So, its a tank simulation. User may press "Space" to fire missile from cannon. Okay that is fine. But what is a tank with only 1 cannon? This big guy has rocket launchers, machine guns, lasers, good stuff. the machine gun and lasers and cannon i know. But the rocket launcher system is slightly mind blocking me. I have the rocket programmed to fly up 30 yards, rotate, and blast towards the target. works great. Problem is selecting the target(s). I would like to have the set up to where when you press the "T" key, the rocket launcher throws a GUI, or some sort of visual sign/icon(Over anything in the way, hills, walls, w.e. is blocking the view from the player to the target, but showing over all possible targets in scene then can press "G" to tab through the targets, then press "Enter" to fire the rocket(Setting the rockets target to the selected target). I've been thinking for a few hours now on how to go about this but comming up with nothing.

Questions:

  1. How to scan for every enemy tank in the scene.

  2. How to tab between those targets.

  3. (Most important) How to display a visual such as GUI, Icon, 2DTexture, Light, something like that, on the targets, making the currently selected have a different visual to show they are the current target.

Example image below.

Any advice?

Feel free to not laugh at my artistic paint ablity.

example.png (9.6 kB)
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

3 Replies

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

Answer by ArkaneX · Mar 24, 2014 at 10:59 AM

1) You can tag enemy tanks with a specific tag, e.g. Enemy (easiest way is to just set this tag on a prefab). When you scan, use FindGameObjectsWithTag to find all of them. . Assign return value to targets array.

2) Create a new variable selectedTargetIndex (int), initially set to zero. When you press Tab, increment it, when Shift+Tab, decrement. If, after increment, it is equal to a number of elements of targets array, set it to zero. If, after decrement, it is lower that 0, set it to number of elements minus 1. This index will allow you to get current enemy tank from your array.

3) You can use GUI.DrawBox, GUI.DrawTexture or any other GUI method. To get proper coordinates, you need to call Camera.WorldToScreenPoint, passing currently selected transform position, e.g.

 Camera.main.WorldToScreenPoint(targets[selectedTargetIndex])

You will probably need to add some offset, but this should be fairly easy.

Another way to highlight, is to change shader on your selected object. You can for example make it red tinted and slowly blinking.

Comment
Add comment · Show 4 · 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 JoshMBeyer · Mar 24, 2014 at 02:33 PM 0
Share

How to increment through the array? I am searching for similar questions but can't find an example

avatar image JoshMBeyer · Mar 24, 2014 at 02:54 PM 0
Share

This is my script, can't find out how to get the cycle through the array with the TAB key to work

 using UnityEngine;
 using System.Collections;
 
 public class T1_TargetSystem : $$anonymous$$onoBehaviour
 {
     public GameObject[] targets;
    
     private int currentTarget;
     private int totalTargets;
 
     public Transform target;
     public Texture2D image;
 
     Vector3 point;
     void Start()
     {
         
     }
 
     void Update()
     {
         target = targets[currentTarget];
         if (currentTarget == targets.Length)
         {
             currentTarget = 0;
         }
         targets = GameObject.FindGameObjectsWithTag("Enemy");
         if (currentTarget == totalTargets)
         {
             return;
         }
         else
         {
             // Find screen position for target
             point = Camera.main.WorldToScreenPoint(target.position);
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Tab))
             {
                 currentTarget++;
             }
         }
     }
 
     void OnGUI()
     {
         Rect rect = new Rect(point.x, point.y, 20, 20);
     }
 }
avatar image ArkaneX JoshMBeyer · Mar 24, 2014 at 03:06 PM 0
Share

If you want to discuss some solution, then please post it under the answer (or under question). Don't create an answer.

avatar image ArkaneX · Mar 24, 2014 at 03:02 PM 0
Share

If by 'incrementing through the array' you mean incrementing a variable described in p. 2, then here's sample code:

 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Tab))
 {
     selectedTargetIndex++;
     if(selectedTargetIndex >= targets.Length)
     {
         selectedTargetIndex = 0;
     }
 }
avatar image
0

Answer by wijesijp · Mar 24, 2014 at 10:15 AM

You can do a raycast to find out the targets you can see..

Comment
Add comment · Show 3 · 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 JoshMBeyer · Mar 24, 2014 at 10:23 AM 0
Share

i said i want to scan for every possible target, meaning every enemy tank in the scene.

avatar image wijesijp · Mar 24, 2014 at 10:50 AM 0
Share

You can find all the tanks in the game by GameObject.FindGameObjectsWithTag("tag_tank"), if you give a tag "tag_tank" to your tank objects

avatar image wijesijp · Mar 24, 2014 at 10:54 AM 0
Share

Are u using NGUI for your UI system?

avatar image
0

Answer by Destran · Mar 24, 2014 at 10:47 AM

To find all possible targets you could do:

 var enemies : GameObject[];
 
 function Update()
 {
 enemies = GameObject.FindGameObjectsWithTag("Enemy");
 }

Note: You don't have to do it in the update, you could make it more like a scan with InvokeRepeating and move it into a seperate function. Or do it in Start() if all your tanks exist from the very beginning.

As far as tabbing between targets and visually representing the selected target, I'd suggest you google "Bergzerg Hack & Slash". I don't remember which video exactly, but he does a 270+ rpg video tutorial and in the first 5-6 videos of the tutorial he covers your 2&3rd question in a video labeled "Targetting" or something like that.

Edit: sorry I was incorrect, it actually starts here--

http://www.youtube.com/watch?v=zNuCvWnGdXA&list=PLE5C2870574BF4B06

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 JoshMBeyer · Mar 24, 2014 at 10:55 AM 0
Share

Thank you for your help. I'll check it out.

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

23 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 avatar image avatar image avatar image

Related Questions

'Mission Complete/Game Over' screen once objective is complete. 1 Answer

How to make the tab key to show/hide GUIlayer 1 Answer

Custom Inspector, Textfield with Icon 1 Answer

Why is it that when i click on a gui element it sends a mousedown and mouseup to the LAST gameobject that i clicked? 0 Answers

Choose a Random Tag 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