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
0
Question by Crilled · Jul 24, 2013 at 03:40 PM · raycastcollidertriggerbullethit detection

Why do my bullets only destroy themselves sometimes after colliding with the wall?

         if (Physics.Raycast(myTransform.position, myTransform.up, out hit, range) &&
             expended == false)
         {
             //if the collider has the tag of Floor then... 
             
             if (hit.transform.tag == "Wall")
             {
                 expended = true;
                 
                 //make the projectile become invisible
                 
                 myTransform.renderer.enabled = false;
                 
                 //turn off the light component
                 
                 myTransform.light.enabled = false;
             }

Pretty much half the time the code works and the bullet gets deleted, but the other half of the time the bullet goes right through the wall. I have no idea why this is happening and would like any input that might solve the problem, thanks.

Comment
Add comment · Show 9
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 BimSekai · Jul 24, 2013 at 03:44 PM 0
Share

Are you sure the bullet actually touches the wall ? If not, check your wall's collider

avatar image robertbu · Jul 24, 2013 at 03:47 PM 0
Share

Need to see more context. Under what circumstances is this code executed? Why 'myTransform.up'...is this the direction the bullets are fired and if so, is it still the direction the bullets are moving when they hit the wall? You talk about colliding, are you using Physics and if so are you getting the OnColliderEnter() event? Is it possible the Raycast() is hitting something other than a 'Wall' just before they go through?

avatar image Crilled · Jul 24, 2013 at 03:54 PM 0
Share

It's not touching anything before the wall and I'm not using any oncolliderenter just if the ray cast comig from the bullet hits something it will delete itself. (The bullet)

avatar image robertbu · Jul 24, 2013 at 04:13 PM 0
Share

I see two possibilities. The first is that the bullet is moving too fast for the 'range' parameter. That is in one frame it is too far away, and in the next frame it is beyond the surface of the wall. You can test this by increasing the range or by slowing the bullet down. The other possibility is that myTransform.up is not facing the wall when the bullet goes through. You can test this by adding

 Debug.DrawRay(myTransform.position, myTransform.up * range);

I can think of several ways to fix either problem.

avatar image robertbu · Jul 24, 2013 at 05:26 PM 1
Share

If the issue is the range/speed, then why is happens will depend on whether you are moving your bullets through the Transform or are moving them Physics. But either way it is a ti$$anonymous$$g issue.

If you are moving through the transform you are likely using Time.deltaTime. This value varies from frame to frame, so how much the bullet moves each frame varies. If you were to record all the positions of the bullet as it flies towards the wall, you would see that no two bullets have the same set of positions. In that case, some bullets are placed so that they detect the wall, and some bullets will skip go from not detecting the wall one frame and past the wall the next.

For Physics movements, you are using a fixed timestep, but that timestep does not align with Update() the same way for each bullet. So the ti$$anonymous$$g of your check with respect to the bullet position will vary with each bullet.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

18 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

Related Questions

Raycast from inside an object 2 Answers

Raycast shooting problems 1 Answer

Ammo Pickup 0 Answers

Object has many cube colliders in different angles and collides with more than one object. 0 Answers

Make raycast ignore some triggers but hit some others? 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