- Home /
Mouse Click + Raycast + Colliders
I've got code that instantiates a laser where the user clicks. However, I have large trigger colliders which are used to detect the player..The mouseclick is registering that it's clicking the invisible trigger and thus when you can't shoot from within the collider. The only info I found was to Edit > Project Settings > Physics > Uncheck "Queries Hit Triggers", but this fix did not work.
Here is the code that shoots the laser:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Vector3 pos = shootFX.transform.position;
Vector3 dir = hit.point - pos;
Instantiate(laserPrefab, pos, Quaternion.LookRotation(dir));
laserShot.Play();
anim.SetTrigger("Shoot");
shoot();
}
How can I make the mouse ignore the triggers and shoot where I actually click?
Answer by N1warhead · Feb 22, 2017 at 01:44 AM
Click on your trigger, click on layer and select ignore raycast.
Answer by ThisGuyThatsHere · Feb 22, 2017 at 01:46 AM
You can raycast on specific layers, so if you create a public LayerMask
variable, move your triggers to unchecked layer and raycast through that layermask you won't hit objects on unchecked layer.
Your answer
Follow this Question
Related Questions
Vector3.Movetoward. Why does this work? 0 Answers
AI Shooting for Aircraft 1 Answer
using A* pathfinding to go to player after raycast has seen it - c# 0 Answers
Change the direction of ScreenPointToRay() Ray 1 Answer
Mouse Movement with physics 1 Answer