x


How do you use a queuing mechanism in Unity?

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 () {

if (Input.GetMouseButtonUp(0))
{
    Vector3 mousePos = Input.mousePosition;
    Ray ray = Camera.main.ScreenPointToRay(mousePos);

    RaycastHit hitInfo;
    if (Physics.Raycast(
        ray, out hitInfo, Mathf.Infinity))
    {
        location = hitInfo.point;
        InTransit = true;
    }
}


if (InTransit == true)
{
    Vector3 Direction = location -
        this.transform.position;

    if (Direction.magnitude > Speed)
    {
        this.transform.position +=
            Direction.normalized * Speed;
    }
    else
    {
        InTransit = false;
    }
}

}

more ▼

asked Apr 15, 2011 at 12:56 PM

GTS925 gravatar image

GTS925
11 15 10 12

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

Apr 15, 2011 at 02:55 PM TowerOfBricks
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Ok, making the next Starcraft killer ;D

You can use a Queue structure...

http://msdn.microsoft.com/en-us/library/system.collections.queue.aspx

//Top of script
using System.Collections.Generic;

//To Declare a generic queue
Queue<Vector3> targetQueue = new Queue<Vector3>();

//In Update or similar
if (theUnitHasReachedItsDestination) {

    if (queue.Count > 0) {
        theNewTarget = targetQueue.Dequeue ();
        //The unit should now move towards this point
    } else {
        //All destination points have been reached
    }
}


//The user has pressed shift and clicked on the point "shiftPoint"
targetQueue.Enqueue (shiftPoint);
more ▼

answered Apr 15, 2011 at 03:12 PM

TowerOfBricks gravatar image

TowerOfBricks
3.9k 50 45 76

I just started C# and I don't really get how this works, can you please explain it just a little?

Apr 17, 2011 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, 2012 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, 2012 at 07:40 PM Bunny83

is there a java version to this?

Oct 15, 2012 at 07:26 PM Sanosake1

was bugging my self how does it not work

for me for Queue this is much better site

let me add

Queue < int > targetQueuex = new Queue < int >();

Queue < classname > targetQueuey = new Queue < classname >();

both work

anyway gr8 answer

Jan 26, 2013 at 09:19 PM sdgd
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2824
x1432
x350
x209
x55

asked: Apr 15, 2011 at 12:56 PM

Seen: 4978 times

Last Updated: Jan 26, 2013 at 09:25 PM