Skip to content

Instantly share code, notes, and snippets.

@arashrasoulzadeh
Created September 5, 2016 18:44
Show Gist options
  • Save arashrasoulzadeh/5b9216712b8f4b879139344bf0d53768 to your computer and use it in GitHub Desktop.
Save arashrasoulzadeh/5b9216712b8f4b879139344bf0d53768 to your computer and use it in GitHub Desktop.
Detect touch on sprite , UNITY
public bool uiTouch (GameObject go)
{
if (go.GetComponent<SpriteRenderer> () != null)
if (Input.GetButtonUp ("Fire1") && go.activeInHierarchy) {
//Gets the world position of the mouse on the screen
Vector2 mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
//Checks whether the mouse is over the sprite
bool overSprite = go.GetComponent<SpriteRenderer> ().bounds.Contains (mousePosition);
//If it's over the sprite
if (overSprite) {
//If we've pressed down on the mouse (or touched on the iphone)
Instantiate (sfxclick);
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment