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 Josh_Stevenson · Nov 09, 2015 at 06:29 AM · physicscollision detection

Collision Detection issue

Hi all, Im really new to Unity (just started a few weeks ago) and Im making a simple FPS game in which the player has a gun and can shoot things. My problem is when I fire a bullet at an enemy, the collision detection sometimes works. Other times the bullet will go right through the enemy. I have the bullet set to detect if it hit an enemy and it works, but like I said, it only works sometimes. Am I using too high a speed for the bullet? Or is it something else? Please excuse my beginner mistakes! :) My code:

 using UnityEngine;
 using System.Collections;
 
 public class Bullet : MonoBehaviour 
 {
     private float speed = 50.0f;
 
     // Use this for initialization
     void Start() 
     {
         //...
     }
     
     // Update is called once per frame
     void Update() 
     {
         transform.Translate(0, 0, speed * Time.deltaTime);
     }
 
     void OnTriggerEnter(Collider other)
     {
         if(other.GetComponent<ReactiveTarget>())
         {
             Debug.Log("Target hit");
         }
         Destroy(this.gameObject);
     }
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rutter · Nov 09, 2015 at 06:31 AM

You may want to set up continuous collision detection. The default "discrete" mode checks collision once per frame, and may miss fast-moving objects like bullets. Continuous collision is more expensive on the CPU, but catches things like this.

Comment
Add comment · Show 2 · 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 Josh_Stevenson · Nov 10, 2015 at 03:53 AM 0
Share

It works just a little better, but the bullet still manages to pass through the enemy at some points.. D: Even with the enemy set to continuous and the bullet to continuous dynamic, the same issue is still present! Any other suggestions or ideas?

avatar image OncaLupe Josh_Stevenson · Nov 10, 2015 at 07:13 PM 0
Share

You can make the collider of the bullet longer, or use a raycast to scan ahead of the bullet each frame.

Stretching the collider would be simpler, and doesn't require changing the visible model or any extra code. It could potentially have issues with rapid fire unless you set the bullets to their own layer and use the Physics $$anonymous$$anager to disable collisions with each other. You would also have to be careful about the collider triggering on the shooter if they start close to it.

Raycasting would allow you to remove the collider and rigidbody from the bullets so there's less work being done by the physics system, and ins$$anonymous$$d scan ahead of the bullet for potential impacts. You'd have to calculate the distance the bullet would travel and have a ray slightly longer than that. It could have issues if the enemy can move fast and the bullet ends up inside the enemy collider, since raycasts don't trigger on a collider if they start from inside it.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make SphereCastAll retrieve all hits with a concave mesh ? 0 Answers

Spawned objects sink through ground, when over 3 clones 0 Answers

How to determine on collision if an object is rotated in a certain way 1 Answer

Intersecting edge colliders cause particles to pass through one edge collider? 1 Answer

How To create 2D collision detection on object which changes dimensions 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