Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ma22be61 · Jun 19, 2015 at 05:18 PM · collisionbullet

Create Bullethole with onCollisionEnter

Hey guys! I am scripting a shotgun and it have a prefab with multiple shots and that gun should have multiple bulletholes. That's what I made :

     void OnCollisionEnter (Collision col)
     {
         if(col.gameObject.name == "Bullet1")
         {
             RaycastHit hit;
             Ray ray = new Ray(transform.position, transform.forward);
             if(Physics.Raycast(ray, out hit, 100f))
             {
                 Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
             }
 
 }
     }


But I don't know why that doesn't work.The bullet prefab also have Rigidbody and sphere collider.

Comment
Add comment · Show 13
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 OctoMan · Jun 19, 2015 at 05:31 PM 1
Share

Where is this script attached to? Is the collider a trigger?

avatar image OctoMan · Jun 19, 2015 at 05:42 PM 1
Share

So you check if the bullet collides with a bullet?

avatar image OctoMan · Jun 19, 2015 at 05:49 PM 1
Share

But like that

 if(col.gameObject.name == "Bullet1")

you test if the hitted collider is a bullet1. How about asking if it hits a wall?

avatar image OctoMan · Jun 19, 2015 at 07:30 PM 1
Share

It is your collider thing.

you need to make your bullet cast the ray in the update or fixedupdate and test if it hits a collider.

 void Update ()
     {
         RaycastHit hit;
         Ray ray = new Ray(transform.position, transform.forward);
         if(Physics.Raycast(ray, out hit, 100f))
         {
             if(hit.collider.tag == "walls")
             {
                 Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
             }
         }
     }

Test this out.

avatar image OctoMan · Jun 19, 2015 at 09:40 PM 1
Share

You're welcome. I just could write this as an answer, but i don't think it useful.

Show more comments

2 Replies

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

Answer by Hellium · Jun 19, 2015 at 07:34 PM

Why do you use a raycast ? Since the OnCollisionEnter has been called, you know that you have collided with something.

After testing the tag / layer of the collided object, If your bullet has hit the wall, you just have to instantiate the bullethole prefab at the following point Collision.contacts[0].point and you also have the normal to the surface with Collision.contacts[0].normal

http://docs.unity3d.com/ScriptReference/Collision-contacts.html

By the way, "It doesn't work" doesn't help at all to understand what's the problem.

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

Answer by Maskeowl · Jan 31 at 09:06 PM

i want to use raycast but i cant i want to see bullet going idk how to do that with raycast

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

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

23 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

Related Questions

Destroy Turret with machine Gun 0 Answers

destroy ''fireball'' on collision 2 Answers

Collision with terrain 0 Answers

A Bullet's rigidbody, hit.normal 4 Answers

Instantiated over the client side by a photonviewed object? 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