- Home /
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;
}
}
}
Your code is not formatted correctly in all places. Formatting helps readability, bad formatting discourages users from answering.
Answer by TowerOfBricks · Apr 15, 2011 at 03:12 PM
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);
I just started C# and I don't really get how this works, can you please explain it just a little?
I'm not the original answerer, but, to explain this code: The word queue comes from the French for 'line' and in program$$anonymous$$g 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 program$$anonymous$$g standard across many organizations to use 'generics' ins$$anonymous$$d of the 'old' collections for these reasons.
@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.
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
Your answer
Follow this Question
Related Questions
Object Movement via Mouse Click? 3 Answers
Interaction between commands waiting onMouseUp() 1 Answer
Using RayCast to Get Mouse Input 1 Answer
How to toggle off / on raycast on a Gameobject 1 Answer
Detect if object clicked on 2 Answers