The question is answered, right answer was accepted
How can I detect touch on a Mesh Collider on an Android device ?
Hi, In my game, upon touching a mesh collider I want the game object of the mesh collider to get destroyed. I am using Unity 5.3.2 free version. I am attaching the code :
void Update () {
foreach (Touch t in Input.touches) {
if (t.phase == TouchPhase.Began) {
if (gameObject.GetComponent<Collider> ().Raycast (Camera.main.ScreenPointToRay (t.position), out hit, Mathf.Infinity)) {
Destroy (gameObject);
}
}
}
}
Try using tags on the object you want to destroy. Then check the raycast using the tag and then destroy the object.
Hi Guys, I tried to run a raycast and check for tag but am not able to destroy the object. please help.Here is the code. I looked at other discussions on the issue but they don't provide a clear cut answer. please note that my game object to be destroyed has a mesh collider and is not convex.
void Update () {
foreach (Touch t in Input.touches) {
if (Physics.Raycast (Camera.main.ScreenToWorldPoint (new Vector3(t.position.x, t.position.y, 1f)), Vector3.forward, out hit, $$anonymous$$athf.Infinity) && (hit.transform.tag == "sharkbody")) {
Destroy (hit.transform.gameObject);
}
}
}
Thank you in advance...@Abhiroop Tandon
I've converted this to a comment because it isn't an answer.
will you please tell us if your gameobject has a collider or not and if your gameobject has a tag sharkbody
and not sharkBody
or SharkBody
, i m asking this because the answer of @Blue Cut is correct
@aditya my game object has a mesh collider(but it isn't convex), and it is tagged sharkbody. another thing...the game object to be destroyed is a child of another game object. does that make a difference? thanks
@aditya i can't see your full post. please add it as a comment and not a reply. thanks
you are getting the full post, the 4 DOTS is the way how i end my comments ... and it will be great if you could send us some of your gameobject and code screenshots
Answer by Blue-Cut · Apr 18, 2016 at 07:58 PM
Hello,
This code should works. (Note that is your not using multitouch, there is no reason to test all the touches, the index 0 will be fine)
void Update()
{
if (Input.touchCount >= 1)
{
// The pos of the touch on the screen
Vector2 vTouchPos = Input.GetTouch(0).position;
// The ray to the touched object in the world
Ray ray = Camera.main.ScreenPointToRay(vTouchPos);
// Your raycast handling
RaycastHit vHit;
if(Physics.Raycast(ray.origin,ray.direction, out vHit))
{
if(vhit.transform.tag == "sharkBody")
{
Destroy (vHit.collider.gameObject);
}
}
}
}
Hi, Sorry, your code didn't work.
I have multiple touches, so i modified it a little.
for (int i = 0; i < Input.touchCount; i++)
{
// The pos of the touch on the screen
vTouchPos = Input.GetTouch(i).position;
// The ray to the touched object in the world
ray = Camera.main.ScreenPointToRay(vTouchPos);
// Your raycast handling
if(Physics.Raycast(ray.origin,ray.direction, out hit))
{
if(hit.transform.tag == "sharkbody")
{
Destroy (hit.collider.gameObject);
}
}
}
I declared all the variables on top, so that's not the problem.
thanks...@Blue Cut
The code I gave to you is not coded in blind, it's from a working case. So I am more than sure it's working. As @aditya said, the typo is important for the tag. Can you try to put some Debug.Log at each step of the function, to check that all is happening as you imagine.
For example in the if of the raycast, check that the collided object is the good one :
Debug.Log (hit.transform.name);
Answer by Waka-Takaki · Apr 18, 2016 at 04:42 PM
Have you tried this:
RaycastHit hit;
void Update () {
foreach (Touch t in Input.touches) {
if (Physics.Raycast (Camera.main.ScreenToWorldPoint (new Vector3(t.position.x, t.position.y, 1f)), Vector3.forward, out hit, Mathf.Infinity)) {
if(hit.transform.tag == "sharkBody") {
Destroy (hit.transform.gameObject);
}
}
}
}
Hi, Thanks for the reply but i don't see how what you said is logically different from what i have tried. bye.
Answer by rsamrry · Apr 24, 2016 at 02:20 AM
Thanks everybody for their contributions. The answer given by @Blue Cut is right. The problem was that my game object's collider was a non-convex mesh collider and they don't respond to raycasts. I simply replaced that collider with a sphere collider and it worked. thanks again.
Follow this Question
Related Questions
buttons vs touch. 1 Answer
Latency with android touch 1 Answer
Spawning enemies after death help 0 Answers
Non-responsive touch controls after showing ads. 0 Answers
Clicker Attack 0 Answers