|
How can I use shift + right-click to queue the unit to multiple locations? I wan't to make a game similar to Starcraft. Here's a code that orders the object to move where the right click is pointed. using UnityEngine; using System.Collections; public class Marauder : MonoBehaviour { void Start () { } public float Speed = 0.1f; private Vector3 location; private bool InTransit; // Update is called once per frame void Update () {
}
(comments are locked)
|
|
Ok, making the next Starcraft killer ;D You can use a Queue structure... http://msdn.microsoft.com/en-us/library/system.collections.queue.aspx I just started C# and I don't really get how this works, can you please explain it just a little?
Apr 17 '11 at 09:18 AM
GTS925
I'm not the original answerer, but, to explain this code: The word queue comes from the French for 'line' and in programming terms this is a 'first in, first out' list. When using a queue, think of it as a line of people waiting for a teller at a bank. The teller can only help one person at a time starting with the first person in line. When people arrive at the bank they join the queue (or are 'enqueued', the line 'targetQueue.Enqueue(shiftPoint);' is the code manifestation of this action). When the teller starts their shift, they call the first person in line up to their counter. Once the person is at the counter, they are no longer in the line and have been 'dequeued'. Similarly, as soon as the teller finishes helping the first person in line, the next person comes out of line or is 'dequeued' (theNewTarget = targetQueue.Dequeue(); ). Once the person is out of the line, they are no longer in the queue and similarly when 'dequeue' is called, the object is removed from the list. In this way, whatever order the items are added to the list is the order the items are 'popped' off of the list (this is also called a 'stack' because you can also imagine it as a stack of papers you are working through, starting at the top of the stack). The other thing to understand about this code is that it is using 'generics'. A 'generic' collection is a collection (such as a stack, queue, array, array list, etc.) that knows about the items held in the list. The standard 'collections' in C# all assume that an 'object' is held within them, but casting must be done to access properties and methods of those 'objects'. The generic collections know exactly what they are holding (the value between '<' and '>' in the declaration tells the compiler this), so the run-time code is faster and more efficient. It also reduces the likelihood of bugs (imagine thinking your queue has 'apples' when it actually holds 'manure'!). It is a de facto programming standard across many organizations to use 'generics' instead of the 'old' collections for these reasons.
May 23 '12 at 06:59 PM
GamesRUs
@GamesRUs: I'm not sure if the user will ever come back since he wasn't online since he posted that comment a year ago. Anyway good explanation.
May 23 '12 at 07:40 PM
Bunny83
is there a java version to this?
Oct 15 '12 at 07:26 PM
Sanosake1
(comments are locked)
|

Your code is not formatted correctly in all places. Formatting helps readability, bad formatting discourages users from answering.