#Unable to destroy gameobject on mouseclick
1 messages · Page 1 of 1 (latest)
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
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
semi-related but I read that Raycast2D can never be null? Hence I need Raycast2D != null? to check if it's working?
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
:teacher: Unity Learn ↗
Over 750 hours of free live and on-demand learning content for all levels of experience!
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.
Oh I've made a couple of project before and published on itch but it's mostly brute forcing my way through
But thanks for the reminder will take a look at the learn unity page
Again
Physics2D.GetRayIntersection is the appropriate function for this, not Raycast
Ooo I'll try thanks
I'm late to respond but thank you so much man
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
nope
whoops my mistake
Physics.Raycast interacts with 3D colliders
Physics2D.Raycast interacts with 2D colliders
ahhh
and this is correct
why is Physics2D.Raycast not appropriate in this case?
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
oh
Ray mouseRay = Camera.main.ScreenPointToRay(mousePosition);
this is firing a 3d ray in this scene from the mouse?
i've used this before when i made a 2d survival game where enemies flood the player from left and right
2d raycast
degenerate?
ohhh
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