Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 gilgada · Feb 22, 2013 at 11:43 AM · raycasttagshitcollide

How does my raycast work?

This is going to be an odd question as I'm essentially asking why something works, instead of why it could be broke.

I am creating a system in which two objects, a prey and a predator react to each other. In order to get these behaviours working I have had to make sure the objects can 'see' each other. I've begun with my prey object and have a raycast set up, checking for an object in range and then drawing a raycast line in debug to show the path between them. This is all handled within this method

 bool HasPreySeenPredator()
 {
     //code to manage direction of prey and whether predator in range
     bool preySeenPredator;
     RaycastHit hit;
     Vector3 fwd = transform.TransformDirection(1,0,0);
     //fwd holds the orientation of the object's 'face'
     
     if(Physics.Raycast(transform.position, fwd, out hit, sightRange))
     {
         preySeenPredator = true;
         Debug.Log("There is something in range. Distance: "
             + hit.distance + ". Object: ");
         Debug.DrawLine(transform.position, hit.point);
     }
         else
     {preySeenPredator = false;}
     
     return preySeenPredator;
 }

This is put together with the help of the Physics.Raycast references in unity's online help files. I have various objects within my scene to which the raycast does not react to. I have the prey tagged as 'prey' and the predator as 'predator' but haven't used their tags in the code. My other objects are untagged and tagging them as 'predator' does not get the prey's raycast to hit it.

Can anyone explain to me why this works?

Comment
Add comment
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

1 Reply

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

Answer by Owen-Reynolds · Feb 22, 2013 at 02:59 PM

Raycasts don't use tags -- they use layers. You probably have everything else in the scene on the ignoreRaycast layer.

To clarify, you can check tags after the ray cast, of what it hits, and you probably want to: "if(phy.rayCst(...) { if hit.CompateTag("prey") ...". But the actual raycast stops at the first collider it hits. You'd never know if prey was behind it.

Layers are uses to make the raycast act like that stuff isn't there. If tigers can "See trough" other tigers, you could make them do that with "phi.rayCst(..., 1<<~(predatLayer || ignoreRCLayer)".

Comment
Add comment · Show 1 · 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 gilgada · Feb 23, 2013 at 12:27 AM 0
Share

Yeah I also found that the difference between the prey and predator and the other objects was whether they had box colliders. Thanks for the info.

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

10 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

Related Questions

if hit.collide.tag is not working on raycast function 1 Answer

Raycast help 1 Answer

Are tag settings passed down from Game Object parent to mesh child? 1 Answer

rayCastHit question 1 Answer

RaycastHit Collider Question 1 Answer


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