I am learning Unity and is trying to convert an XCODE game i have.
I have a deck of cards with 61 sprites on top of each other:
…and I am using Raycast to try to capture the first sprite and I am not able to do that?
I also have a drag function to be able to move the sprite around.
I get very strange results:
- When i click on the top-sprite, the second on top is selected (60 of 61)
- When i then drag (works) the sprite it is the 51th sprite (51 of 61) that is dragged according to the print
I just do not get my acts together around this “simple” challenge and really need help. I have been spending quite a few hours on this and tested all kinds of stuff but still ends up with the same problem. The current code is just the basic to try to sort this out.
Script attached to an empty GameObject:
void Update() {
if(Input.GetMouseButtonDown(0)) {
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
print (hit.collider.tag);
}
}
The “drag” attached to the sprites:
public class dragTheStuff : MonoBehaviour {
float x;
float y;
void Update() {
x = Input.mousePosition.x;
y = Input.mousePosition.y;
}
void OnMouseDrag() {
// Control the drag of the sprite
transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (x, y, 1.0f));
print ("Drag: " + transform.tag);
}
}
All help is appreciated.