• 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 IntelDev94 · Oct 29, 2014 at 01:34 AM · prefabs

How to detect a collision from a prefab bullet

I have a FPS shooter the fires bullets (prefabs). I have a cube that I would like to be able to fire bullets at that I can detect it being shot and will then be destroyed.

I added a GameObject (a cube) called 'Cube'. The OnTrigger is unclicked. I have the following script attached to the Cube Object:

using UnityEngine; using System.Collections;

public class shootCube : MonoBehaviour {

 void OnCollisionEnter (Collision col)
 {
     if(col.gameObject.name == "Cube")
     {
         Debug.Log ("Hit Cube!");
         Destroy(col.gameObject);
     }
     else 
     {

         Debug.Log("Hit something else!");
     }
 }

}

The bulletPrefab is set-up like this: Tag=theBullet Cylinder 001 (Mesh Filter) Meah -> Cylinder001

Animator (checked) Culling Mode= Always Animate

RigidBody: Collision Detection = Discrete

Bullet Hit (script) Script bulletHit

Here is the bulletHit Script:

using UnityEngine; using System.Collections;

public class bulletHit : MonoBehaviour {

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }
 void OnCollisionEnter(Collision col)
 {
     Debug.Log ("Bullet hit something!");
 }

}

Comment
Add comment · Show 1
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 robertbu · Oct 29, 2014 at 01:36 AM 0
Share

$$anonymous$$ake sure that one of the two objects has a non-kinematic rigidbody. Slow your bullets down to make sure speed is not the issue. If speed is the issue, solutions have been posted many times. $$anonymous$$ake sure the colliders are aligned with the mesh and enabled.

For future posts, please format your code. After pasting, select your code and use the 101/010 button to format.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by VellaDev · Oct 29, 2014 at 01:50 AM

You should use a ray cast for a bullet, because if the object velocity is to fast it won't detect a collision

If you can't do that then turn your collision detection up on the object and slow it down, u can also use OnTrigger if the obj is set to is trigger

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 robertbu · Oct 29, 2014 at 01:51 AM 0
Share

http://wiki.unity3d.com/index.php?title=DontGoThroughThings

avatar image IntelDev94 · Oct 29, 2014 at 03:58 AM 0
Share

I tried the first suggestion setting speed to very slow but it didn't seem to detect anything. I changed the script on the Cube to:

using UnityEngine; using System.Collections;

public class shootCube : $$anonymous$$onoBehaviour {

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     
 }
 
 void OnCollisionEnter (Collision col)
 {
     if(col.gameObject.name == "bulletPrefab")
     {
         print ("Hit the Cube " + col.gameObject.name);
         Destroy(col.gameObject);
     }
     else 
     {

         print ("Hit Something " + col.gameObject.name);
     }
 }

}

As soon as the game starts I see "Hit Something Terrain". Is there a problem with the this part of the script col.gameObject.name == "bulletPrefab" ? Should I be checking for the name of the bulletPrefab?

Both the Cube and the bulletPrefab are non-kinematic is that O$$anonymous$$?

Thank you.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Count Prefabs on builded .apk 0 Answers

Multiplayer different player prefab 0 Answers

PrefabUtility does not exist? 1 Answer

how can i make prefabs keep there inspector varibles 1 Answer

(Re-posted) How to save multiple of the same prefab clones 0 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