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
1
Question by darryldonohoe · Jul 08, 2012 at 04:02 PM · raycastingmaskraylayer

How to detect certain object using raycast

Hi guys,

I have a problem which i can't figure out. I want to do something like this:

If my camera's raycast*(field of view) hits a certain object(enemy) it will run an action(for now just a debug.log)*. But it doesn't ignore other objects. So if a wall stands in front of the enemy. The raycast doesn't notice the enemy.

I have searched on google and UnityAnswers, and came up with masklayers. But i didn't knew how that worked.

 var layerMask = 1 << 8;

I tried the reference code that they used:

     function Update () {
 
   // Bit shift the index of the layer (8) to get a bit mask
 
   var layerMask = 1 &lt;&lt; 8;
 
   // This would cast rays only against colliders in layer 8.
 
   // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
 
   layerMask = ~layerMask;
 
   var hit : RaycastHit;
 
   // Does the ray intersect any objects excluding the player layer
 
   if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity, layerMask)) {
 
     Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * hit.distance, Color.yellow);
 
     print ("Did Hit");
 
   } else {
 
     Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) *1000, Color.white);
 
     print ("Did not Hit");
 
   }
 
 }

But again, i dont know how to set up a layer in the script.

Can anybody help me with this? Or knows a tutorial?

Thanks in advance! Darryl Donohoe

Comment
Add comment · Show 2
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 darryldonohoe · Jul 08, 2012 at 08:23 PM 0
Share

I tried this one:

var target: Transform; // the target transform private var hit: RaycastHit;

function Update() {

 var fwd = transform.TransformDirection (Vector3.forward);

 if (Physics.Raycast (transform.position, fwd, 10)) hit.transform == target;

     Debug.Log("There is something in front of the object!");

} But even this script displays the log when it hits an object. Doesn't matter which one.

avatar image darryldonohoe · Jul 08, 2012 at 09:01 PM 1
Share

So i think i got it!

Here is the code

function Update() {

var hit: RaycastHit; var forward = transform.TransformDirection (Vector3.forward);

 if (Physics.Raycast (transform.position, forward, hit))

 if(hit.collider.CompareTag("AISpy")){

Debug.Log("hit");

}else{

// Something blocking line of sight

// Enemy should engage if out of sight

}

// Show in the editor the ray

Debug.DrawLine (transform.position, hit.point);

}

But since this is a single raycast, it isn't that accurate. Is there a faster and a more efficient way to produce more raycasts without telling what each one has to do?

Like for my AI i have 3 raycasts and i had to script what happened if the left right or middle raycast hit an obstacle. But it would be easier, if i had something like a function. And if one of the raycasts hit the object, it would check the function ins$$anonymous$$d. So that i wouldn't have to copy every action

2 Replies

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

Answer by aldonaletto · Jul 08, 2012 at 04:48 PM

There's a much easier way to check visibility: use a Linecast from the character to the target, and check if the hit object is the target - if not, something is in between:

  var target: Transform; // the target transform
  ...
  private var hit: RaycastHit;
  if (Physics.Linecast(transform.position, target.position, hit) && hit.transform == target){
    // target is visible
  }
If you want to also check if the target is inside the viewing angle, use this:

var viewAngle: float = 120; // define the viewing angle

function CanSeeTarget(target: Transform): boolean { var hit: RaycastHit; return Vector3.Angle(transform.forward, target.position-transform.position)

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 darryldonohoe · Jul 08, 2012 at 06:04 PM 0
Share

If i understand correctly, the linecast would check even though you are facing a different direction.

But the viewAngle isn't working for me, i also tried the one in the reference section. But that one can see through walls.

I have to say that i am new to scripting. I followed some basic tutorials. And after that, tried to make this work. I also tried the onBecameVisible function, but that one can also see through walls. I am working on this for about a week now and still can't figure it out.

avatar image
0

Answer by KMHarder · Dec 03, 2014 at 06:08 PM

thank you, you've saved my life ^

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

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

7 People are following this question.

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

Related Questions

Upper limits of Raycasting; Physics engine "gives up" 0 Answers

How to make raycast go through various colliders 1 Answer

How to make a ray always shoot forward from Camera viewpoint. 1 Answer

Layer Mask Not working to open the door 1 Answer

What is the proper way to blend between two Mecanim States in a Layer, when both animation clips have different Masks? 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