Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Section-One · Oct 31, 2015 at 05:18 PM · raycastlayermask

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?

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zharik86 · Oct 31, 2015 at 06:09 PM 0
Share

I think, you need use function RaycastAll and find in hits tag by player.

avatar image Section-One zharik86 · Oct 31, 2015 at 06:33 PM 0
Share

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.

avatar image Cherno zharik86 · Nov 01, 2015 at 12:22 PM 2
Share

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();
avatar image contab009 · Oct 31, 2015 at 06:23 PM 1
Share

You could try to cast the ray from an empty gameobject from outside the collider.

avatar image Section-One contab009 · Oct 31, 2015 at 06:34 PM 0
Share

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

avatar image Bunny83 Section-One · Oct 31, 2015 at 08:01 PM 0
Share

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?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Section-One · Nov 02, 2015 at 08:51 AM 0
Share

raycasting from inside the collider seems to do the trick

avatar image lodendsg · Feb 20, 2018 at 01:21 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

37 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges