- Home /
Making a raycast ignore specific collider
Hello,
I have 100 turrets. each turret have a collider and sends a raycast to the player. If i stand behind the turret, the raycast hits the turret's collider first.
I understand about making it ignore a layer, but i don't want it to ignore all colliders on the other 99 turrets. just it's own. and obviously i don't create a layer for each turret.
so how do i get it done?
I think, you need use function RaycastAll and find in hits tag by player.
thought about that, but the results are in no particular order as far as i see, so i have no idea if it hits the player before it hits any other objects or not. and if it does hit another object before me then i need it to not shoot.
You can easily order the hits like this:
using System.Linq;
RaycastHit[] hits = Physics.RaycastAll(ray, distance, layer$$anonymous$$ask).OrderBy(h=>h.distance).ToArray();
You could try to cast the ray from an empty gameobject from outside the collider.
so simple. why didn't I think about that. won't be the most precise thing to do, but better then nothing and i would have to use 4 emprty gameobjects. one for each side of the collider. if i just put one on top then at certain angles it would still hit the collider. (the player can also go above and below the turret). hope that doing so many raycasts won't affect my performance so much
Uhm, isn't the turret rotating towards the target? The empty gameobject should be rotated with the turret. You literally place it at the point where your projectile will left your "barrel".
If you don't rotate the turret and just want to check if a certain target can be seen / reached by the turret, you can simply offset the start position by the approximate size of your turret. Just add your normalized raycast direction to the origin. You can multiply the normalized direction by the radius around your turret where you want to start your raycast.
A screenshot might be more clear how your setup actually looks like. I guess it's a 2d game due to the "4 directions"? What kind of collider does the turret have?
Answer by OncaLupe · Oct 31, 2015 at 08:19 PM
Raycasts won't trigger on a collider if they start inside it, so if you're using one collider for your turret you can simply start the raycast from inside it. If you really do want to start the raycast outside the turret though, you don't really need to use empty gameobjects, just a bit of math.
using UnityEngine;
using System.Collections;
public class TurretRaycast : MonoBehaviour
{
public Transform player;
Vector3 raycastOrigin;
Vector3 raycastDirection;
float raycastStartRange = 2f; //How far from the center of the turret to start raycast
RaycastHit hitInfo;
void Update()
{
raycastDirection = (player.position - transform.position).normalized;
raycastOrigin = transform.position + (raycastDirection * raycastStartRange);
if(Physics.Raycast(raycastOrigin, raycastDirection, out hitInfo))
{
//Check hit
}
}
}
Note that if the player stands directly against the turret using that code, the raycast origin could be within the player's collider, so if possible have the origin inside the turret collider.
raycasting from inside the collider seems to do the trick
For others incase its useful.
The method we used to deal with a similar situation is to test if the player was hit and if so cast the ray again from the point found in the previous cast shifted forward by a small amount to insure we are in fact inside the players collider.
We used this route as opposed to layers as the player can take control of any ship at any time so its not feasable to use layers to ignore the 'player' as that can be any given object at any given point.
in psedo code that would be
if(Physics.Raycast(ray, out hitInfo))
then
if (hitInfo.rigidbody == activeShip.selfBody)
then
ray.origin = hitInfo.point + activeShip.selfTransform.forward * 0.05f
if(Physics.Raycast(ray, out hitInfo))
then
//test for player hit again or just assume it cant possibly be the player this time depending on your game
else
//Hit nothing
end if
else
//Hit something that wasn't the player
end if
else
//Hit nothing
end if
Your answer
![](https://koobas.hobune.stream/wayback/20220612043440im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast and Layer Masks 1 Answer
How can I prevent my raycast from passing through UI? 3 Answers
My raycast ignores layer mask, could you give me some advice? 1 Answer
Usage of Layer masks, what am I doing wrong? 1 Answer
Layer Mask Detection 2 Answers