- Home /
Drag/drop potentially unlimited generated "creep" objects, but one at a time
What I currently have is a script to drag a 2D sprite. The problem is, the script runs off of the object to be dragged's tag.
However, If I have, for example, seventy of these same objects, then clicking and dragging on one will drag all objects with that tag.
To be more specific, my end-goal is to have potentially endless creeps (meaning several duplicates) spawning. clicking on any one creep and dragging it will only drag that one creep. Will I need a ton of tags to achieve this or is there a better way?
Here is my current code. If you reply with code, feel free to use either javascript or C#, I'm not picky.
Thanks for your input(s)!
#pragma strict
function Update ()
{
if (Input.GetMouseButton(0))
{
var hitInfo : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hitInfo.collider.tag == "Guard")
{
this.transform.position.x = hitInfo.point.x;
this.transform.position.y = hitInfo.point.y;
Debug.Log("object clicked: "+hitInfo.collider.tag);
}
}
}
What I see in your code is it sets position of this object to place where you clicked on object with Guard tag.
You are only picking one object, checking if it has "Guard" tag, and doing something. If you click on object without "Guard" tag it won't do anything. If you click on several objects with "Guard" tag only one will respond to that click.
You don't need that $$anonymous$$onoBehaviour on all creeps. Ins$$anonymous$$d, you need your camera(or some neutral single object) to check if you clicked on Creep and get it if you did.
Interesting. I can't believe I didn't think of this. Thank you very much!