#Unable to destroy gameobject on mouseclick

1 messages · Page 1 of 1 (latest)

viral timber
#

I'm trying to destroy the red circle when a mouse is clicked using raycast2d and comparing the gameobject tag

The red circle (tagged as "Trash") should be destroyed

#

Currently, i have made the mouse to cast a ray, detect a click on the screen and check whether the object clicked has the "Trash" tag

I have also set the camera as the "MainCamera" tag, and the red circle has a 2d box collider with the "Trash" tag

#

from the various unity forums I've read

#

Here's my code

#

semi-related but I read that Raycast2D can never be null? Hence I need Raycast2D != null? to check if it's working?

normal junco
#

There are so many issues in that code it feels like you've no idea what you're doing at all. I'd recommend going over the unity beginner pathways before you start working on your own project.

#

!learn

velvet laurelBOT
normal junco
#

The main issue is that in 2d you don't cast rays to detect objects at mouse position. You use overlap point or similar queries, because you literally want to see if the mouse position is overlapping any objects in 2d space.

viral timber
#

But thanks for the reminder will take a look at the learn unity page

#

Again

gusty basalt
viral timber
#

it's been long fixed

#

I just wanna take note of this in case i need to use this in the future

#

from what I've read Physics2D.GetRayIntersection casts a 3d ray that interacts with 2D colliders

#

but Raycast itself just interacts with any type of collider, whether it be 3d or 2d

viral timber
#

whoops my mistake

gusty basalt
#

Physics.Raycast interacts with 3D colliders
Physics2D.Raycast interacts with 2D colliders

viral timber
#

ahhh

viral timber
#

why is Physics2D.Raycast not appropriate in this case?

gusty basalt
#

because you're firing a 3D ray from the camera at 2d objects

#

You are talkiung about a 2D raycast with ZERO length which is really not a raycast at all at that point

#

The raycast should be imagined as like aiming a laser pointer in the scene and seeing what it touches

viral timber
#

oh

Ray mouseRay = Camera.main.ScreenPointToRay(mousePosition);
this is firing a 3d ray in this scene from the mouse?

gusty basalt
#

from the camera

#

towards the point the mouse is aiming at

viral timber
#

i've used this before when i made a 2d survival game where enemies flood the player from left and right

#

2d raycast

#

degenerate?

gusty basalt
#

i mean when you do a raycast with zero lenght

#

that's degenerate

viral timber
#

ohhh

gusty basalt
#

so it's just a point

viral timber
#

ahhh

#

if you don't mind me leaving for a bit to find the time I use a physics2d.raycast, just so I could make sure I still remember what physics2d.raycast did

#

a short gif of putting it into practice

#

I'm not sure whether this is a right use of the physics2d.raycast, but i made a raycast at a fixed length that affects the enemy ai.

If the raycast returns the gameobject tag player, it pursues the player until it's close enough to attack

#

if the player leaves the raycast fixed range, the enemy goes back to its wandering behaviour