- Home /
How to detect a touch on a 2D sprite
Hi, I realize there's a lot of questions for this topic already but none of them really fixed this for me. Maybe I'm just doing it completely wrong, I am very much a noob with unity.
Anyway, I have a 2D sprite on which I have added an Edge collider 2D. I'm trying to detect when I touch it, on my touch device.
I tried to follow another post I found here, but it never registers a touch. Here's the code I have so far.
private Collider2D collider;
// Use this for initialization
void Start()
{
collider = GetComponent<Collider2D>();
}
// Update is called once per frame
void Update()
{
CheckForTouch();
}
bool CheckForTouch()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
var wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
var touchPosition = new Vector2(wp.x, wp.y);
if (collider == Physics2D.OverlapPoint(touchPosition)){
Debug.Log("HIT!");
}
else{
Debug.Log("MISS");
}
}
return false;
}
Answer by Chik3r · Jul 10, 2018 at 03:23 PM
Maybe the edge collider isn't working (You have to touch the edge?), try using a polygon colider
Oh wow, that sounds likely. I thought the edge collider was what I wanted but now that I read you comment it all makes sense. Hah. Thanks, of course the edge collider is for edge only. I feel like a moron.
I'll check if this is it, when I get back home, later today.
can you put script when i touch 2d collider the animation state change and when not the animation state return to the default
So, this was pretty much the answer. If you want to put it in as an answer, ins$$anonymous$$d of a comment, I can mark it.
Answer by Bubinga_Studios · Jul 10, 2018 at 03:33 PM
@Nicoolai If you are going for a UI perspective, you can use the Button component and use the On Click () function to state what you would like it to do (NOTE: The void function you are calling via the button must be a public void ).
Otherwise, if you are using sprites and want to recieve input when target sprite is touched (from what I am seeing that is what you want your code to do), you may want to look into void OnMouseDown()
function and do something like this.
void OnMouseDown() { Debug.Log("HIT!"); }
More can be read about OnMouseDown() here. You can also check OnMouseUp() so the target function is only called when you release your mouse button https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
If I did not answer your question the way you were looking for, just reply asking whatever I missed :)
Hope this helped!
Thank you very much, i'll give that a shot when I get home later today.
Regarding the On$$anonymous$$ouseDown(), will that work with touch, ins$$anonymous$$d of a mouse? Or only mouse clicks?
Sorry for late response. On$$anonymous$$ouseDown() will only work with mouse clicks. If you want to see if one collider comes in contact with another, try out one of these
For Colliders (2D): void OnCollisionEnter(Collision2D col) calls script only once when collision occurs void OnCollisionStay(Collision2D col) calls script every frame while in collision void OnCollisionExit(Collision2D col) calls script only once when collision ends
For Triggers (2D) void OnTriggerEnter(Collider2D col) calls script only once when entering trigger void OnTriggerStay(Collider2D col) calls script every frame while in trigger void OnTriggerExit(Collider2D col) calls script only once when object leaves trigger
So for example: if I wanted to find out if I collided with an object tagged "Enemy" and it IsAlive (A boolean in a script called $$anonymous$$onster) by finding the collision: void OnCollisionEnter(Collision2D col) { if(col.gameObject.tag == "Enemy" && col.GetComponent<$$anonymous$$onster>().IsAlive) { //Do whatever is needed here :) } }
Again, if this wasn't clear, you can check the documentation here:
NOTE: The Collider/Collision does not need to be named "col" it can be whatever you want. SORRY ABOUT LINE SPACING!
Your answer
Follow this Question
Related Questions
Finding the centre of two touches 2 Answers
Unity2D touch screen input 0 Answers
2 fingers sprite rotation 1 Answer