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 Arustyred · Jul 13, 2018 at 05:50 AM · raycastcollidercollision detectionraycasthitray-casting

Raycast goes through collider

Hello,

I'm using raycasts to shoot a rocket. However, it goes straight through colliders sometimes. I checked some of the basics and made sure my code was in FixedUpdate and made sure my distance wasn't too small. Also, tried debugging rays and they go straight through the collider and still no collision. Here is my code:

 void FixedUpdate () {
         movement = (forward.normalized * force) - downMovement + boost;
         nextPosition = raycastSpot.position + movement;
         distance = Vector3.Distance (raycastSpot.position, nextPosition);
         distance += 10f;
         rotation = Quaternion.LookRotation (movement.normalized, Vector3.up);
 
         Debug.DrawRay (raycastSpot.position, movement, Color.white);
 
         RaycastHit hit;
         if(Physics.Raycast(raycastSpot.position, rotation.eulerAngles, out hit, distance)){
                     //This is not called    VV
                     hitSomething (hit.point, hit.normal);
         }else{
             transform.position = nextPosition - (movement.normalized * offset);
             transform.rotation = rotation;
         }
 
         Debug.DrawRay (raycastSpot.position, movement, Color.blue);
 
         if (boost.y > 0f) {
             boost -= boostSubtract;
         } else {
             boost = Vector3.zero;
         }
     }

"boost" is just a vector3 adding some upward force at first that is taken away over a few frames by "boostSubtract"

The blue ray is just to give me and idea where the rocket will be next frame, even though it is slightly off.

The white ray tells me exactly where is was at the start and end of the frame.

Pictures: alt text

alt text

It does work sometimes, I have two more pictures showing it colliding into the terrain, but I cant upload any more pictures on this post.

pic2.png (285.3 kB)
pic1.png (496.1 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 $$anonymous$$ · Jul 13, 2018 at 05:57 AM 0
Share

Is the collider marked as a trigger in the inspector? If it is then it probably should be set to not being marked as a trigger.

Also, is the collision box actually the same size as the cube your trying to hit in those pictures? Its possible they are colliding just doing so inside the mesh, resulting in it being unclear if they have or not.

Other than those two possible causes I don't really know whats going on here.

avatar image Arustyred $$anonymous$$ · Jul 19, 2018 at 04:12 AM 0
Share

Whoops, I didn't see your comment until just now. The collider is the same size as the cube and is not a trigger. Thanks for replying.

avatar image Arustyred · Jul 19, 2018 at 04:08 AM 0
Share

Nothing new to report. Still haven't found a solution.

avatar image Ali_Jaffer Arustyred · Jul 19, 2018 at 06:12 AM 0
Share

Did you check your Physics Settings of your project. Check Layer Collision $$anonymous$$atrix and also make sure that cube has not layer of "Ignoredraycast"

avatar image Arustyred Ali_Jaffer · Jul 21, 2018 at 06:39 AM 0
Share

I checked and the cube and rocket are both on "default" and the collision matrix had collision between the layers, I'm thinking it's something with my code that I'm not seeing.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jul 21, 2018 at 09:19 AM

Physics.Raycast needs a position vector for the starting point and a direction vector as direction, You passed in the eulerAngles of that object. This is not a direction at all. It's just a collection of 3 angles. If you want to cast a ray into the forward direction of your object, just use transform.forward

 if(Physics.Raycast(raycastSpot.position,transform.forward , out hit, distance)){

However since you actually use "movement" vector to move your object you may just use this vector as it's the direction you would move along

 if(Physics.Raycast(raycastSpot.position, movement, out hit, distance)){
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
avatar image
1

Answer by AdityaViaxor · Jul 21, 2018 at 11:43 AM

make sure that your collision detection in rigidbody in continues it is often mistake that we just add rigidbody and the collision detection is by default set it discrete to sometime it collide and sometime it doesn't also check whether both are not set to isTrigger ture this also a reason for passing through object.

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 KellyeAnnora · Jan 20, 2020 at 04:21 PM 0
Share

Came here with the same problem and this was my issue. Thanks for having commented this in 2018.

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

143 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 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 avatar image avatar image avatar image

Related Questions

Physics raycast hit offset from where it should be 1 Answer

Raycast exit point of collider 0 Answers

RaycastHit collider not showing correct tags 1 Answer

Raycast passes Collider 0 Answers

Raycasting isn't working anymore (C#) 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