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
0
Question by D3m0nE · Apr 19, 2013 at 06:06 PM · c#bulletshoot

Problem With Bullet Prefab

Hello Everyone I Got Problem, that my Bullet Ignore The Gameobject and go throw thim, without any effect

Shooting Code :

 if(Input.GetButton("Fire1") &&  Time.time > nextFire ){
 
 nextFire = Time.time + CurrentWeapon.firerate;
             
             
 
             Rigidbody clone;
             clone = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.SpawnPoint.position, CurrentWeapon.SpawnPoint.rotation) as Rigidbody;
             bulletprefab = clone.GetComponent<Bullet>();
             bulletprefab.Range = CurrentWeapon.Range;
               clone.velocity = transform.TransformDirection(Vector3.forward * CurrentWeapon.BulletSpeed );
 
 }



And in Bullet Prefab i have this Code :

 //In Update 
 
 
             Vector3 Directoion =  transform.TransformDirection(Vector3.forward);
             if(Physics.Raycast(transform.position,Directoion,out hit,Range * 100)){
             if(hit.rigidbody){
             hit.rigidbody.AddForceAtPosition(transform.TransformDirection(Vector3.forward) * Range,hit.point);
             }
 
 
 //OnTriggerEnter(Collider Col){
 
     Vector3 hitUpDir  = hit.normal;
         Vector3 hitPoint = hit.point + hit.normal * -0.02f;
             try{
             
             switch(Col.tag)
                     {        
             case "terrain":
                 Destroy(myTransform.gameObject);
                 networkView.RPC("DirtParticle",RPCMode.All,hitPoint,(Quaternion.FromToRotation(Vector3.up,hitUpDir)));
                 break;



Now When i Shoot the Terrain, sometimes it work and show up the DirtParticle and many other time the bullet just go throw the terrain without any effect

Comment
Add comment · Show 3
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 AlucardJay · Apr 19, 2013 at 06:10 PM 0
Share

There is a chance that the bullet is moving too fast for the collision to be entered. Try setting the rigidbody Collision Detection setting to Continuous Dynamic

avatar image D3m0nE · Apr 20, 2013 at 12:59 AM 0
Share

thanks ,, but still the same

when i shoot terrain some hit show the dirt particle and other just go throw the terrain

avatar image Lockstep · Apr 20, 2013 at 02:13 AM 0
Share

From my experience the collision detection doesn't help at all.

You could solve this problem by brute force. Use Physics.Linecast ever fixed frame to check if something was between the new and the old position of your bullet. This might impact performance if you have many bullets. There is even a script on the wiki which does this. It is not very well coded but you can learn from it and it will likely work for something spere shaped.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Raydaq · Apr 20, 2013 at 02:20 AM

This is probably due to the speed of the bullet as alucardj mentioned.

You could try implementing this script from the wiki (I haven't tried it myself but others have had success with it) DontGoThroughThings Script

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 CG-DJ · Apr 20, 2013 at 07:29 AM

You could also use raycasts to simulate bullets. A raycast is a line spawned from a point and shot out in one direction until it hits something. When it hits something, you can use that data to do all sorts of things!

 //The place where the bullet spawns from, probably an empty
 var spawnPoint : Transform;
 
 //If you want your want your bullet to have a maximum distance
 var maxDist : float;
 
 //Your particles for dust
 var dust : GameObject;
 
 //Your particles for blood
 var blood : GameObject;
 
 function Update()
 {
     if(Input.GetButton("Fire1") &&  Time.time > nextFire )
     { 
         nextFire = Time.time + CurrentWeapon.firerate;
         
         //variable to store RaycastHit info
         var raycastHitInfo : RaycastHit;
         
         //variable to store which direction is forward
         var fwd : Vector3;
         fwd = spawnPoint.transform.TransformDirection(Vector3.forward);
         
         //Make the raycast
         if(Physics.Raycast(spawnPoint, fwd, raycastHitInfo, maxDist)
         {
             //In here is where you put what you want to bullet to do
             //If it hits your terrain, then it will instantiate dust
             if(raycastHitInfo.collider.name == "terrain")
             {
                 Instantiate(dust, raycastHitInfo.transform.position, raycastHitInfo.transform.rotation);
             }
             
             //Or, if you bullet hits an enemy
             if(raycastHitInfo.collider.tag == "enemy")
             {
                 // prepare rotation to instantiate blood or sparks
                 var rot = Quaternion.FromToRotation(Vector3.up, raycastHitInfo.normal);
                 Instantiate(blood, raycastHitInfo.transform.position, rot);
             }
         }
     }
 }     


Here are the wiki links too! http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html

I really hope this helps! I worked hard on the code above. Plus me up if it helps!

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 Raydaq · Apr 20, 2013 at 02:50 PM 0
Share

Raycasts are a good way good doing this as well, however, you cant really have guns that fire bullets at different velocities. (the bullet will be instant).

avatar image D3m0nE · Apr 20, 2013 at 07:44 PM 0
Share

thank u, but i dont need that

because i want a bullet prefab to show up too

and when that bullet prefab hit the terrain, the Dust show up

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

15 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

Related Questions

How to Sync bullet spawn with shoot animation 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

bullet damage problem 1 Answer

Bullets Shot Stay Still 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