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 vexe · Oct 28, 2013 at 05:24 PM · raycastrayphysics.raycast

Raycast help: casting 3 rays, out of the faces of a cube

Hey guys, I have a cube that I'm casting 3 rays from. A graphical representation would be best.

alt text

(I don't know what's up with the uploading, but there's no cuts in the vertical ray, they're all straight lines, at least that's what I see in my screen shot...)

Anyway, now those are drawn via Debug.DrawRay - Here's how I'm doing it:

 public enum Direction { Up, Right, Forward }
 Direction[] dirs = { Direction.Forward, Direction.Right, Direction.Up };
 int index = 0;

 void Update()
 {
     index = (index + 1) % dirs.Length;
     UpdateEndAndStartPoints(dirs[index]);
     Debug.DrawRay(start, -mDir * length * 2, Color.blue);
 }

 void UpdateEndAndStartPoints(Direction dir)
 {
     switch (dir) {
         case Direction.Forward:
             mDir = mTransform.forward;
             break;
         case Direction.Right:
             mDir = mTransform.right;
             break;
         case Direction.Up:
             mDir = mTransform.up;
             break;
     }
     start = mTransform.position + mDir * length;
     end = mTransform.position - mDir * length; // I'm not using end anywhere for the moment, previously I used it to identify the 2nd point of my line renderer
 }

Now what I'm trying to do, is to cast real rays, instead of just debug ones, so that if a face of my cube hits a floor or something, I could detect that. So I thought I could just add:

 void Update()
 {
     index = (index + 1) % dirs.Length;
     UpdateEndAndStartPoints(dirs[index]);
     Debug.DrawRay(start, -mDir * length * 2, Color.blue);

     RaycastHit hit;
     if (Physics.Raycast(start, -mDir, out hit, length * 2)) {
         if (hit.collider.tag == "Floor")
             print("I: " + this + " just hit: " + hit.collider.name);
     }
 }

Yes, I didn't forget to give my cube a "Ignore Raycast" layer, since Physics.Raycast detect one object, if I don't do that I'd have to go for Physics.RaycastAll.

Now, it's not working right, in some faces, the ray that's 'supposedly' coming out of it, isn't seem to hitting the floor, although the visual one is. Although I have the same start, direction and length as in my Debug.DrawRay method.

alt text

If I rotate the cube, other faces hit the ground, and gives output, while others don't.

I'm pretty sure it's something related to the way I'm casting the rays, but shouldn't it work, since I'm casting it just like I'm doing in my Debug.DrawRay?

What am I missing?

Thanks for any help.

archery.png (26.1 kB)
ray.png (29.0 kB)
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 sethuraj · Oct 28, 2013 at 05:50 PM 1
Share

I had the same problem once,Debug.DrawRay() and Physics.RayCast() behaves differently.

Initially my code wuz like this

         if (Physics.Raycast(transform.position,Ray_Direction,out Ray_Hit)) 
         {
             //Get the ray hitting point
             Ground_Pos=Ray_Hit.point;
             
             //Draw the ray which is casted by the raycaster
             Debug.DrawRay(transform.position,Ground_Pos,Color.green);
         }
 

This give me wierd results.The ray-casting is fine and ground is detecting but the ray ending is drawing somewhere else.

Then I changed my code to like this drawing the ray properly and ray-casting works fine.

 if (Physics.Raycast(transform.position,Ray_Direction,out Ray_Hit)) 
         {
             //Get the ray hitting point
             Ground_Pos=Ray_Hit.point;
             
             //Draw the ray which is casted by the raycaster
             Debug.DrawRay(transform.position,Ray_Direction*Ray_Hit.distance,Color.green);
         }
avatar image vexe · Oct 28, 2013 at 05:58 PM 1
Share

@sethuraj +1 good point! calling Debug.DrawRay AFTER the ray hits something, to see what it hit and get a visualization of the actual ray.

@robertbu sorry I'm in a hurry right now and must go. I will get back to you with proper feedback after a while.

2 Replies

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

Answer by robertbu · Oct 28, 2013 at 05:51 PM

As a start, just inside your your Raycast() put:

 Debug.Log(hit.collider.name + ", " + hit.collider.tag);

This will tell if your raycast is hitting something you don't expect.

I didn't forget to give my cube a "Ignore Raycast" layer, since Physics.Raycast detect one object

Meshes are one sided. If you Raycast() from inside the cube for example, you will not hit the cube. While I don't think it is related to your problem, it appears you are moving your Raycast to the outside face of the cube. You don't have to do that. You can just cast from the center point. In fact moving to the face can cause you some problems since objects (with default Unity settings) are allowed to interpenetrate. This would mean that it is possible for a cube that is face down on a surface to not detect the hit.

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 vexe · Oct 28, 2013 at 08:28 PM 0
Share

$$anonymous$$eshes are one sided

That's it all it took to say for me to know my problem! The problem was, since I'm casting say, my up ray, (which goes from the bottom of my cube, intersects it and go up) if I now place my cube naturally on the plane it won't detect the hit, just like you said cause the ray is starting from underneath the cube going up, thus going through the outer-side of the plane, not the inner.

Using a cube as a floor, just like @EvilWarren said lets me get away with it, and using only 3 rays.

I didn't wanna use 6 rays and cast from the center, cause I thought I'd get away with 3 ins$$anonymous$$d. But I just discovered that for my purposes, for what I'm trying to do, it's better to use 6 anyway.

Thank you and @EvilWarren for your answers.

avatar image
1

Answer by EvilWarren · Oct 28, 2013 at 06:47 PM

You need 6 rays for all your sides. Extending it to start on the other side of your cube won't work if your floor is a plane, as the collision mesh for a plane is single sided. Also, because your directions are negative, your forward ray is back, your up is down and your left is right. If you are using some sort of volume for your floor collider like a box, then it might work if your collider is thin enough. I wouldn't suggest using a box collider though. Probably better to add 3 extra rays.

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 EvilWarren · Oct 28, 2013 at 06:58 PM 0
Share

Also, I would follow robertu's advice and start the rays from the centre of the cube.

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

17 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

Related Questions

Trying to figure out if one object can see another 1 Answer

How to send a Ray through a vertex? 0 Answers

Trouble with Ray & Raycasting and AddForce() 1 Answer

Why does ScreenPointToRay bug when called from IEnumerators? 1 Answer

Raycasting and foreach loops 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