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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Yanifska_ · May 05, 2013 at 08:16 PM · raycastraycasthitmultiple

ray cast returning the wrong object, going through multiple objects

Dear community.....

I have a rough FOV object attached to an enemy gameobject.
When the fov object collide with the character, it sends a ray from the enemy to the character and check if there is an obstacle between them.

Unfortunately it seems like it's returning the last object instead of the first one, or objects at a specific distance from the caster.

Did I miss some aspect of RayCast ? or something else with my script ? Maybe is it not returning objects that are too close ? I am considering using RayCastAll but it seems too heavy for this simple task....

Thanks in advance for any help...

I am attaching a screen capture showing a ray going through a box then through the character and finally into a building the raycast is returning the building. I need it to return the box object.

alt text

Here is the code attached to the FOV object :

 #pragma strict
 
 private var script : enemyScript;
 
 function OnTriggerStay(collided  : Collider) {
     
     if(collided.gameObject.name == "Character"){
 
         var hit : RaycastHit;
         if ( Physics.Raycast(transform.position, collided.transform.position, hit, 10)) {
         
             Debug.Log (hit.transform.gameObject.name);// it should return the obstacle
             Debug.DrawLine (transform.parent.position,  hit.point, Color.red);
             if(hit.transform.gameObject.name == "Character") {
             
                 transform.parent.GetComponent(enemyScript).state = AIState.Chasing;  
             }
         }            
     }    
 }
 
 function OnTriggerExit(collided2  : Collider) {
     
     if(collided2.gameObject.name == "Character"){
     
         transform.parent.GetComponent(enemyScript).state = AIState.Investigating;              
     }    
 }


capture.jpg (245.2 kB)
Comment
Add comment · Show 5
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 ExpiredIndexCard · May 05, 2013 at 08:31 PM 0
Share

Does the box have colliders?

avatar image Fattie · May 05, 2013 at 08:32 PM 0
Share

google unity3d physics layers

avatar image Yanifska_ · May 05, 2013 at 08:39 PM 0
Share

yes it does, it is not a trigger though, I just tested setting it up as a trigger but nothing's changed

avatar image Yanifska_ · May 05, 2013 at 08:49 PM 0
Share

I understand that layers allow to isolate group of colliders, I am not sure if its what I need.... But I just found that I can allow raycast to interact with triggers, and it seems to work better that the standard box collider. So maybe I will set my colliders as triggers and isolate them on a layer so the rays won't interact with non wanted triggers. will keep you updated

avatar image Yanifska_ · May 05, 2013 at 09:05 PM 0
Share

hmm there 's something wrong with my setup now, I didn't try the trigger option, but my ray is not anymore pointing toward my player as it should....

1 Reply

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

Answer by robertbu · May 05, 2013 at 09:34 PM

This form of Raycast takes a position and a direction, so it should be:

    if ( Physics.Raycast(transform.position, collided.transform.position - transform.position, hit, 10)) {

 

But you may want to use Linecast() instead:

    if ( Physics.Linecast(transform.position, collided.transform.position, hit, 10)) {
 
Comment
Add comment · Show 3 · 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 Yanifska_ · May 06, 2013 at 06:26 PM 0
Share

that is very helpful, Linecast behaviour is better but still problematic. it seems to shoot only if my target is colliding with other objects....I also deactivated shooters's collider and rigidbody in inspector just in case...

avatar image Yanifska_ · May 06, 2013 at 06:31 PM 0
Share

oops sorry, my mistake it's seems to be working almost fine. I was expecting the Linecast to be shot even when there are no obstacle. The last problem is if my character is close to another object, like against a wall, but not behind it, the linecast return true. Do I need to shoot it just a little before the target ? or do I need to change the target collider ?

avatar image Yanifska_ · May 06, 2013 at 06:39 PM 0
Share

sorry for spam$$anonymous$$g, but I believe I can fix all that by adjusting the colliders. Thank ya all !

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

12 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

Related Questions

Is this way of making a raycast correct 2 Answers

RaycastHit2D.collider is null - why? 1 Answer

How do I change the raycasthit material back? 1 Answer

Raycasting will not hit some imported meshes with mesh colliders? 2 Answers

Physics.Raycast not hitting anything 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