- Home /
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:
How to scan for every enemy tank in the scene.
How to tab between those targets.
(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.
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.
How to increment through the array? I am searching for similar questions but can't find an example
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);
}
}
If you want to discuss some solution, then please post it under the answer (or under question). Don't create an answer.
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;
}
}
Answer by wijesijp · Mar 24, 2014 at 10:15 AM
You can do a raycast to find out the targets you can see..
i said i want to scan for every possible target, meaning every enemy tank in the scene.
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
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
Your answer