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
2
Question by yyamiyyugi · Dec 05, 2013 at 10:19 PM · c#bulletgunweaponpenetration

Bullet Penetration

Ok, so I have been playing with creating guns in unity and I want to create bullet penetration, I have seen a few ideas on how to implement this but they are all for raycast guns, I'm spawning a bullet object and launching it. The only way I can think to do this would be to make the collider for the bullet a trigger upon colliding an object with the tag "Penetrable", However this rarly actually works and it causes the whole game to lag, any ideas? Script: using UnityEngine; using System.Collections;

 public class BulletObject : MonoBehaviour {
 
     void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.tag == "Penetrable") 
         {
             collider.isTrigger = true;
             print (this.gameObject.name +" Has had it's spherecollider removed");
         }
 
         if (collision.gameObject.tag == "Terrain")
         {
             print (this.gameObject.name + " has hit the ground!");
             Destroy (this.gameObject);
         }
 
     }  
 
     void OnCollisionExit(Collision collision)
     {
         if (collision.gameObject.tag == "Penetrable") 
         {
             collider.isTrigger = false;
             print (this.gameObject.name + " Has had it's spherecollider re-added");
         }
     }
 }
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
3
Best Answer

Answer by aldonaletto · Dec 06, 2013 at 02:16 AM

If you want to pass through things, let the bullet be a trigger all the time and use OnTrigger events instead. You can add a regular rigidbody to it and apply forces or set rigidbody.velocity in order to move the bullet. If you want it to bounce off of non-penetrable surfaces, however, things may become complicated: you should calculate yourself the hit point and reflect the velocity about the normal. Another problem you may have is the "tunnel effect", when a fast rigidbody simply passes through thin colliders without generating any collision or trigger event (the rigidbody is before the collider in one physics cycle and after it in the next).

Fortunately, there's a solution that solves both problems: let the bullet be a simple mesh (no collider), calculate the trajectory each frame and check collisions with Linecast. The bullet script could be like this:

 using UnityEngine;
 using System.Collections;
 
 public class BulletScript: MonoBehaviour (
   public float bulletSpeed = 100;
   public float gravity = 9.8f;
   public float range = 1000;
  
   IEnumerator Start(){
     Vector3 vel = bulletSpeed * transform.forward; // calculate velocity vector
     Vector3 pos = transform.position;
     float dist = 0; // initialize distance travelled
     while (dist < range){
       yield return null; // let Unity free till next frame
       vel.y -= gravity * Time.deltaTime; // apply gravity
       Vector3 newPos = pos + vel * Time.deltaTime; // calculate current position
       float thisDist = Vector3.Distance(pos, newPos); // get the distance since last frame
       dist += thisDist; // update total distance
       // check collision
       RaycastHit hit;
       if (Physics.Linecast(pos, newPos, out hit)){ // if something hit...
         switch (hit.transform.tag){
           case "Penetrable":
             // object is penetrable
             print("Penetrating " + hit.transform.name);
             break;
           case "Terrain":
             // hit the ground: delete bullet (coroutine dies too)
             Destroy(gameObject);
             break;
           default:
             // other objects - bounce off:
             vel = Vector3.Reflect(vel, hit.normal); // reflect velocity
             thisDist -= hit.distance; // calculate distance from surface
             // find new position after bouncing:
             newPos = hit.point + thisDist * vel.normalized;
             break;
         }
       }
       transform.position = pos = newPos; // update position
       transform.forward = vel; // align bullet to the movement direction
     }
     // shot ended because it's out of range
     Destroy(gameObject); // bullet suicides
   }
 }

In order to create the bullet prefab, get the bullet object you already have and remove collider, rigidbody and everything else but the MeshFilter and the MeshRenderer. Attach the script above to the bullet and drag it to the Project view to make it a prefab. When you want to fire the bullet, just instantiate it at the desired position and pointed to the target - the bullet moves by itself.

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 Avatar01 · Jun 10, 2015 at 06:54 AM 1
Share

Thank u Aldonaletto. it was useful for me.

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

17 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

Related Questions

Shooting multiple bullets 2 Answers

Creating laser gun effect. 1 Answer

Bullets aren't firing / Bullet holes not instantiating correctly 0 Answers

Multiple Cars not working 1 Answer

Bullet flies away for no apparent reason. 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