Created
September 5, 2016 18:44
-
-
Save arashrasoulzadeh/5b9216712b8f4b879139344bf0d53768 to your computer and use it in GitHub Desktop.
Detect touch on sprite , UNITY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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