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 DharkenKnight · Jun 22, 2017 at 12:19 AM · raycasting

Problem getting the right raycast hit

I've got a game object at about +10 Y that triggers, among other things, a Raycast when the 'player' gameobj breaks the collider. It has a spotlight on it which I have tracking the gameobject, and to try and understand when the gameobject has 'line of sight' on the player, I implemented a ray cast.

But something isn't right. Though I am staying on the player object with both the spotlight and a debug ray, when (I think) using the same targeting approach with the raycaster .- it only seems to register hits on the player object at a pretty dramatic distance.

In the images here, the spotlight is green simply when the object is tracking the player, but turns red while the raycaster is registering hit. As you can see, even though both the debug ray and spotlight have no problems hitting the player, the raycaster is pretty spotty about it - or appears to be..

First, the images: alt text

This one shows the raycast hitting the player at a pretty extreme distance, however this one:

alt text

Shows the spotlight and the debug ray hitting the player, but the color of the light (and a public bool, and debug messages) show that the ray is not hitting it. (blocked by something else maybe?)

In short, extensive checks indicate the debug ray and spot are both hitting - ray cast is not. Do raycasts stop at the first thing they hit? My understanding was they kept going and returned 'hits' on everything in their path.

Here is the code I am using to a) follow the player with the spot, b) cast a debug ray and c) the raycaster itself.

     void LookAtTarget(Transform t)
     {
 
         Vector3 targetDir = t.position - scanrayHousing.position;
         float step = 10f * Time.deltaTime;
         Vector3 newDir = Vector3.RotateTowards(scanrayHousing.forward, targetDir, step, 0.0F);
 
         scanrayHousing.rotation = Quaternion.LookRotation(newDir);
         RaycastHit hit;
 
         if (Physics.Raycast(scanray.position, newDir, out hit))
             print("Ray hit-->" + hit.transform.gameObject.name + " at " + hit.distance.ToString());
         Debug.DrawRay(scanray.position, newDir * 10, Color.green);
         if (hit.transform.gameObject.tag == "Player")
         {
             UpdateScanRay(pursuitColor);
             lineOfSite = true;
         }
         else
         {
             UpdateScanRay(alertColor);
             lineOfSite = false;
         }
     }

Keep in mind, I have been messing with this quite a bit, changing the transform position originating the spot, debug ray etc - all have produced variations on the same thing but for posterity:

the object is a simple sphere (scanray) and as a child, a empty game object which acts as the parent to the spotlight (scanrayhousing). I did these as a way of segregating the rotation of the parent object from the child.

Any help would be appreciated - I'm certain I am missing something elementary.

P.S. The raycaster does return hits, and does hit the player object, but with a huge angle of space below the object where it cannot seem to hit the player object.

screen-shot-2017-06-21-at-80028-pm.png (118.1 kB)
screen-shot-2017-06-21-at-80013-pm.png (186.5 kB)
Comment
Add comment · Show 1
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 hexagonius · Jun 22, 2017 at 12:35 AM 0
Share

The raycast stops at the first hit. If you want all hits use Phydics.RaycastAll.

1 Reply

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

Answer by aldonaletto · Jun 22, 2017 at 03:46 AM

Raycast stops at the first collider it hits, so if any collider is in between, the ray won't hit the player - but your code should show the name of the object which's blocking the ray. Part of your code should be inside the if - the way it is now keeps returning old info stored in the hit variable even when nothing is hit. You can let the debug ray outside the If, so that you'll know what the ray is aiming at:

 ...
  Debug.DrawRay(scanray.position, newDir * 10, Color.green); // move this line outside the if
  if (Physics.Raycast(scanray.position, newDir, out hit)){
      // only use the info stored in hit if the ray hit something:
      print("Ray hit-->" + hit.transform.gameObject.name + " at " + hit.distance.ToString());
      if (hit.transform.gameObject.tag == "Player") {
          UpdateScanRay(pursuitColor);
          lineOfSite = true;
      } else {
          UpdateScanRay(alertColor);
          lineOfSite = false;
      }
    }
 }





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 DharkenKnight · Jun 22, 2017 at 12:35 PM 1
Share

Thanks so much. I should have known that when the raycast hit was not an array.

So, what I did not mention was that the sphere has a rather large box collider that is offset from the sphere (imagine a large box being dragged along the ground), this collider is acting as the sensor to pick up player collides - well - because of its size, it was automatically the first thing being hit until the players angle vs the spheres angle (at about 10.y).

Your comment put me on the right track - when the player collision is detected, in the ontriggerenter event I'm going to immediately resize the collider so that the raycast has the proper angle of attack on the player.

Thank you again!

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

70 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 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

C# Raycast goes straight into the air 2 Answers

how to setup raycast firing system in this situation 1 Answer

if ( Physics.Raycast ( ray, out hit, Mathf.Infinity, LayerMask ) ) not working? 2 Answers

Raycast off empty gameobject rather then camera. 2 Answers

Set raycast on player camera or object? 0 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