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 /
  • Help Room /
avatar image
0
Question by unity_rookie · Jan 12, 2016 at 10:03 AM · collisionraycastraycasthit

Raycast not detecting hit

For fast moving objects OnCollisionEnter doesn't work always. To solve this issue I thought raycast is best, but raycast is also not detecting hit.

 if (Physics.Raycast(trans.position, trans.forward, out hit, 0.1f,)){
             if(hit.transform != null){
                 Debug.Log("tag : "+hit.transform.tag);
             }
 }

This is not giving hit. Is there a solution to overcome this issue?

Note - I want the maxDistance = 0.1f. The object which is casting ray is not having any collider and the object is moving with a speed of 1000f.

 rigidBody.AddForce(transform.forward * 1000f);

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 savlon · Jan 12, 2016 at 11:38 AM 0
Share

Have you drawn the raycast to make sure it's piintihg in the right direction?

avatar image unity_rookie savlon · Jan 12, 2016 at 11:44 AM 0
Share

Yeah i have checked using debug.drawline and the direction was right

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Lukas-Brunner · Jan 12, 2016 at 01:15 PM

OnCollisionEnter() should work even for fast moving objects if you change the collosion detection in the rigidbody component to "continous". The only drawback should be the higher cpu cost.

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 unity_rookie · Jan 13, 2016 at 05:36 AM 0
Share

If I hange the collosion detection in the rigidbody component to "continous" it is working fine. As you mention it has a drawback. So this method is not optimized.

avatar image Lukas-Brunner · Jan 13, 2016 at 08:34 PM 0
Share

well i guess the only other thing you could do, is to decrease the timestep of the physics engine, but this has the same effect on the cpu as my first suggestion. If your really cannot afford compromises and u are a real pro you could maybe write a compute shader and do the collision detection on the graphics card on your own. But don't ask me how ;) ...

avatar image unity_rookie · Jan 14, 2016 at 05:06 AM 0
Share

Your suggestion about shader seems intereseting but I am not a pro. It will take some time to do that. For the time being I am using linecast. $$anonymous$$ay be I will use raycast. Not sure though.

avatar image
0

Answer by UniqueMAX · Jan 12, 2016 at 12:23 PM

Here's my list of possible solution, hope it heps =)

  1. Try playing your project frame-by-frame using the button on the right from Pause button in editor. Check if there's at least one frame where your ray actually goes through the object.

  2. Try using the script without if(hit.transform != null). Physics.Raycast already returns a non-null value on hit. You can simply check hit's tag directly to know if it's the object you need.

    if(hit.tag=="MyTag"){ Debug.Log(hit.tag); }

  3. Are you sure you need such a short MaxDistance? Maybe your object is simply too far from the raycast source? Try this method: Raycast without MaxDistance and when it hits make it return the distance on hit.

Debug.Log(Vector3.Distance(trans.position,hit.transform.position));

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 lassade · Jan 12, 2016 at 02:08 PM 1
Share

Adding some other possible solutions:

  1. In Project Setting/ Physics (2D or not) check the Layer Collision $$anonymous$$atrix, make sure the layers can hit each other (maybe this does nothing to do with a Raycast, but check any ways)

  2. Try use the layer mays as -1 (everything) i notice there is a coma before the distance but no value, maybe the compiler is assu$$anonymous$$g 0 (nothing).

Use Gizmos to see if the distance is enough to a hit happen

avatar image unity_rookie lassade · Jan 13, 2016 at 05:32 AM 0
Share
  1. Collision matrix is not a problem. I have checked that, When both are on default layers, still it is not working.

  2. Tried this method as well. Same output.

avatar image unity_rookie · Jan 13, 2016 at 05:30 AM 0
Share

I had already tried some of these methods.

  1. Frame by frame I had already tried. The thing is when the force amount is 1000 it is not detecting anything. When the force is 100 hit is detecting 3 times.

  2. Tried without if(hit.transform != null). Same output.

  3. Yes I need such a short distance. Without maxdistance it is working fine. But with this it will detect before the collision and I don't want that.

Distance method option is one way of doing it. Still I feel this is not the right way.

avatar image
0

Answer by thesleeve · Jan 12, 2016 at 03:40 PM

Are you sure that the objects are on layers that are set to interact with each other in the physics engine? If the objects are on separate layers, then you must click the corresponding check box on the collision matrix (Edit->Project Settings->Physics). See http://docs.unity3d.com/Manual/LayerBasedCollision.html for more details.

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 unity_rookie · Jan 13, 2016 at 05:37 AM 0
Share

Yes the objects are on same layer. I have checked with default layer as well. The end result is the same.

avatar image
0

Answer by Veerababu.g · Jan 13, 2016 at 07:05 AM

it's not working because of distance you are giving i guess.

do ray casting with infinity and check the distance. may it works in your case

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 unity_rookie · Jan 14, 2016 at 05:07 AM 0
Share

Checking distance is a good idea. I will try that.

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

9 People are following this question.

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

Related Questions

How do you detect a mouse button click on a Game Object? C# 2 Answers

Adding collision to Raycast hit 0 Answers

Make raycast ignore hitbox? 0 Answers

How to freeze an GameObject on x axis in specific direction? 1 Answer

Why doesnt the value change? 2 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