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 GTS925 · Apr 15, 2011 at 12:56 PM · raycastmouseclickpointqueue

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;
 }

}

}

Comment
Add comment · Show 1
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 TowerOfBricks · Apr 15, 2011 at 02:55 PM 2
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
4

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 &gt; 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);

Comment
Add comment · Show 5 · 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 GTS925 · Apr 17, 2011 at 09:18 AM 0
Share

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

avatar image GamesRUs · May 23, 2012 at 06:59 PM 1
Share

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.

avatar image Bunny83 · May 23, 2012 at 07:40 PM 0
Share

@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.

avatar image Sanosake1 · Oct 15, 2012 at 07:26 PM 0
Share

is there a java version to this?

avatar image sdgd · Jan 26, 2013 at 09:19 PM 0
Share

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

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

3 People are following this question.

avatar image avatar image avatar image

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


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