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 DeveLOLper · Mar 11, 2014 at 05:14 PM · rigidbodybulletglitchthrough

Bullet goes through object

I have a "bullet" (just a little cube). It's got all the colliders and everything. Whenever I shoot it to a box in-game, which has a box collider and rigidbody, the bullet goes straight through it. Only occasionally does the box fall back as it is hit. Help?

Comment
Add comment
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

2 Replies

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

Answer by wibble82 · Mar 11, 2014 at 05:30 PM

Physics engines like the one in unity operate by default in 'discrete' mode. This means that each frame it looks at the position of each object and checks if any are overlapping. If they are, a collision occured. It does it's stuff, then moves each object forwards by its velocity.

In your case I would imagine your bullets are moving very fast, so its entirely possible they manage to be on 1 side of the target on 1 frame, but pass right through to the other side in a single time step (cos their velocity is so high), so end up on the other side of the target without ever having overlapped it.

The simplest but most computationally expensive method of fixing this is to use continuous collision detection as documented here: https://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-collisionDetectionMode.html

However, for bullets I often use a much simpler approach. If your bullets are very small and are moving in a straight line, you don't really need them to be full rigid bodies. Instead, just make them not collidable (they don't even need a collider or a rigid body). Store inside them a 'move speed' and each frame:

  • calculate the position you'll be in next (transform.position+move_speed*Time.fixedDeltaTime)

  • do a ray cast from current position to next position

  • if the ray cast hits something, your bullet has collided and do stuff!

  • if the ray cast doesn't hit something, move your bullet to the next position

Just like with collisions you can choose to ignore certain results and use layers to filter your ray cast.

That's the standard 'cheap' way to do bullets, as it avoids doing complicated physics. You're ultimately just treating the bullet as a fast moving point, rather than a body with volume. Since bullets are usually pretty small there's no noticeable effect.

I don't have code to hand which is a shame cos I've got a game at home doing just this. Ray casts are well documented though so a few google searches should tell you what you need to know.

Note: This MUST be done inside a FixedUpdate function, not an Update function, as it runs in tandem with the physics and you might see some weirdness if you just leave it in Update.

Extra note: If your bullets are moving REALLY fast, and you want to save a little performance, you can often just do a ray cast when they are first fired, get the hit position then move them to that point over a few frames. Technically if the target moves the result is no longer correct but for objects that reach their target in a fraction of a second this is rarely noticeable. Handy for things like machine guns where you'll have lots of bullets flying around at once. Most shooters use this approach - what/whether you hit something is determined the moment you click fire, and the moving bullet is just a graphical effect.

Extra extra note!: And if you want your bullets to still have a physical effect on your target, check out AddForceAtPoint. You can calculate where your bullet hit the target, then apply a small force there so it gets pushed around when hit by a mailstrom of metal!

Comment
Add comment · Show 6 · 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 fafase · Mar 11, 2014 at 05:38 PM 0
Share

I would go for Linecast ins$$anonymous$$d of Raycast.

 Vector3 prev = Vector3.zero;
 
 void FixedUpdate(){
    if(prev != Vector3.zero){
       RaycastHit hit;
       if(Physics.Linecast(prev,transform.position, out hit)){
          // Coliision happened at hit.point
       }
       prev = transform.positoin
 
    }   
 }
avatar image DemSec fafase · Jun 15, 2016 at 10:50 PM 0
Share

By using this method, I can't reference the object with other.gameObject. How could I achieve the same procedure with this method?

avatar image fafase DemSec · Jun 17, 2016 at 07:48 AM 0
Share

the collided object is stored in the RaycastHit struct.

  hit.collider.gameObject;
avatar image wibble82 · Mar 11, 2014 at 05:52 PM 0
Share

Yeah line cast is better - didn't realise that was in there. I'd still do away with rigid bodies and colliders altogether though. Unnecessary complication unless you need the bullets to bounce around and rotate.

avatar image DeveLOLper · Mar 12, 2014 at 04:39 PM 0
Share

Thanks you helped :)

avatar image Brijs · Jun 16, 2016 at 11:24 AM 0
Share

To move bullet do not use transform, use rigidbody methods only.

avatar image
0

Answer by hidro636 · Mar 11, 2014 at 05:48 PM

My guess is that the velocity of the bullet is putting it on the other side of the collider instead of actually colliding with it. For example, if the "bullet" is moving at 10 units per frame and the object that it's aimed at is only 5 units long, the cube is going to "jump" past the collider depending on the distance it was fired from.

Not entirely sure what a good fix would be except for possibly casting a ray from the rear of the bullet and checking to see if that ray is colliding with the object it was supposed to hit.

Hope that helps a bit

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 DeveLOLper · Mar 12, 2014 at 04:39 PM 0
Share

You helped too, cheers

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

25 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

Related Questions

Multiple Cars not working 1 Answer

Bullet Sometimes Go Throw Object 0 Answers

Bullet Hole not inline with sight?(Center of Screen) 1 Answer

dont instantiate clones 1 Answer

Hinge Joint goes crazy 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