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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Ubicray · Sep 04, 2014 at 12:37 PM · 2dcollision detectionplatformer

Collision detection not working as intended

Hey all, newbie here I am trying to make a basic 2D platformer and getting help from a book named "Unity 2D game development" So I have a bullet prefab with box2D, and a collider for my enemy object. Using events and delegates for hit detection script(Know it's an overkill, just book stated so) Problem is, when I am shooting an enemy, it doesn't always kill him despite the simple Destroy(gameObject, 0.1f) command. Sometimes it hits and kills, but sometimes it doesn't(Bullet gets lost, so I guess collision is made) Any idea why?

Comment
Add comment · Show 4
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 bhartu · Sep 04, 2014 at 12:56 PM 1
Share

please post your code for clarification.

avatar image Ubicray · Sep 04, 2014 at 01:26 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class TakeDamageFromPlayerBullet : $$anonymous$$onoBehaviour
 {
     public delegate void hitByPlayerBullet();
     public event hitByPlayerBullet hitByBullet;
     
     void OnTriggerEnter2D( Collider2D collidedObject )
     {   
         if(collidedObject.tag == "Player Bullet")
         {
             if(hitByBullet != null) hitByBullet();
         }
     }    
 }

avatar image Ubicray · Sep 04, 2014 at 01:27 PM 0
Share

@bhartu,

 public void hitByPlayerBullet()
     {
         GameObject deathFxParticle = (GameObject)Instantiate(deathFxParticlePrefab);
         Vector3 enemyPos = transform.position;
         Vector3 particlePos = new Vector3(enemyPos.x,enemyPos.y,enemyPos.z +1.0f);
         deathFxParticle.transform.position = particlePos;
         Destroy(gameObject,0.1f);
     }

avatar image OrbitGames · Sep 04, 2014 at 01:29 PM 0
Share

I think your bullet is too fast and small, so it isn't above the enemy collider.

Check if changing the collision detection to continuos works

1 Reply

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

Answer by Pyrian · Sep 04, 2014 at 01:28 PM

Probably the bullet is getting destroyed before the collision registers with the enemy. This is a common problem when making collisions that destroy both participants. Instead of destroying them in the event, simply set a Boolean flag, and destroy them in Update.

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 Ubicray · Sep 04, 2014 at 02:06 PM 0
Share

Thanks! Changed bullet's code to Destroy(gameObject,0.1f); and it helped

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

Scale object, collision not working 2 Answers

Layers of collision, or multiple collisions 1 Answer

2D Platformer Raycasting vs. Rigidbody collisions 0 Answers

2D platformer - character clips into the ground 1 Answer

Player sinking into walls and floor with Raycasts 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