How can I detect a touch on a custom button and object in Unity3d
I am trying to make a game for Android and iPhone where if the player clicks a 3d chest, he will recieve some items. I have done the item giving, but I cannot detect a touch properly. Also it has to have a support for multiple touches, so the payer can keep pressing the move button, while opening the chest.
Answer by CaptainKirby · Sep 01, 2015 at 11:47 AM
So you want to check if you touched a 3d object, correct?
Simply get the mouse/touch position and cast a ray from the camera to the position and see if it hits a chest to open.
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
if hit.collider.CompareTag("Chest"))
//do stuff
}
}
But with this if I try and delete the chest after being opened, can I get its coordinates or something? (The game is generated at random so it all chests are at the same tag).
Well if you delete the game object then you have to store its position in an array of a sort, if you wish to know where it was.. But maybe explain why you want to save the position, then maybe there is a better solution :)
Your answer
Follow this Question
Related Questions
How do I get a the movement ofan arm like in truck loader game? 0 Answers
Vector3.back not working 2 Answers
How to replace KeyCode.Space with a simple tap on Android? 1 Answer
roller Ball for android 0 Answers