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 RexoniuM · Dec 18, 2012 at 11:39 PM · rigidbodyobjectgravity

My rigidbody gets destroyed

I have made bullet positioned inside a barrel and denoted as rigidbody. Now it all goes fine, however all suddenly it gets off the barrel and destroys itself. How to prevent this from occurring, should I wrap it with some object that will clamp it in the tight position or what?

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 clunk47 · Dec 18, 2012 at 11:49 PM 1
Share

Do you mean a gun barrel or a storage barrel? And why do you have a bullet inside of it? If you mean a bullet in a gun you mean to fire, you don't want to position it inside the gun. You want to create a bullet prefab then instantiate a clone of it when you fire the gun. If this is what you mean, let me know and I'll post an answer with some code to get you started.

avatar image RexoniuM · Dec 18, 2012 at 11:57 PM 1
Share

Yes, I meant gun barrel, clunk47! :) O$$anonymous$$, so your point is I shouldn't position it at all inside a barrel, but to do what? I am a bit confused. I shouldn't represent physically at all my bullet in idle momentum, but only when the mode for firing is triggered, and I do that by creating clones from the object which is what?

avatar image clunk47 · Dec 19, 2012 at 04:55 AM 1
Share

Ok since you've explained what you want to do, which is have a gun and fire a bullet from it right? Tell me if you prefer C# or JavaScript(UnityScript), and I will set up a script and explanation for you :)

avatar image RexoniuM · Dec 19, 2012 at 01:07 PM 1
Share

I prefer doing in JS, but C# is just fine for me as well, clunk47! :)

2 Replies

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

Answer by clunk47 · Dec 19, 2012 at 04:50 PM

Make a new C# script and name it FireTest and use this code for it. Attach this code to either the main camera, or an empty gameobject at the end of the gun barrel. Make sure the z axis of this object at the end of the barrel is facing forward.

 using UnityEngine;
 using System.Collections;
 
 public class FireTest : MonoBehaviour 
 {
     public GameObject bullet;
     GameObject clone;
     Vector3 fwd;
     public float speed = 300.0f;
     public float lifeTime = 2.0f;
     
     void Start () 
     {
     
     }
     
     void Update () 
     {
         fwd = transform.forward;
         if(Input.GetMouseButtonDown (0))
         {
             //Instantiate a clone of the bullet prefab.
             clone = (GameObject)Instantiate(bullet, transform.position + fwd, Quaternion.identity);
             
             //Add a rigidbody component to the clone if one does not exist.
             if(!clone.rigidbody)
                 clone.AddComponent<Rigidbody>();
             
             //If and when a rigidbody component is added or already exists, set parameters.
             else if(clone.rigidbody)
             {
                 //Disable the bullet's use of gravity to make travel smoother.
                 clone.rigidbody.useGravity = false;
                 
                 //Set collision detection on fast moving objects like this bullet to be Coninuous and Dynamic.
                 clone.rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
                 
                 //Freeze rotation of the rigidbody (bullet).
                 clone.rigidbody.freezeRotation = true;
                 
                 
                 //Add an impulse force to the bullet.
                 clone.rigidbody.AddForce(fwd * speed, ForceMode.Impulse);
                 
                 //Check if THIS transform is a collider.
                 if(collider)
                     //If this is a collider, the bullet should not collide with it.
                     Physics.IgnoreCollision(clone.collider, collider);
                 
                 //Destroy bullet clone after lifetime expires.
                 Destroy (clone.gameObject, lifeTime);
             }
         }
     }
 }
 
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 clunk47 · Dec 19, 2012 at 04:53 PM 2
Share

Note. Be sure to create a PREFAB in the project pane and drag / drop your bullet onto it, and then delete the original bullet from the SCEN$$anonymous$$ Drag the PREFAB onto the script's "Bullet" slot.

avatar image RexoniuM · Dec 19, 2012 at 09:42 PM 2
Share

Great script, clunk47! I will try to modify it to suit it better my needs, but in general this could be a good template for everyone wanting to create it's own bullet system. I liked especially idea of creating bullet in the flow! :)

avatar image
0

Answer by Alec Thilenius · Dec 19, 2012 at 07:53 AM

You want to have an empty transform as a child of the gun, and positioned at the end of the barrel. When gun fires, you want to Instantiate a new bullet prefab. If it has a ridged body and collier, then set the bullets speed after it is instantiated.

Bullets are not actually a trivial thing to properly implement. You need to be aware that the physics system is not stable at the speeds that bullets move at. The best solution is to keep track of the bullets 'lastPosition' in the fixed update loop, and draw a ray from the 'lastPosition' to the current position and see what it hits.

The (Much) easier solution is just to draw a ray from the end of your gun and see what it hits. Don't create a bullet anything.

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 RexoniuM · Dec 19, 2012 at 01:09 PM 0
Share

Yes, but I have already implemented bullet firing system. I just can't drop it now, after all the work and time invested in it. What it hits I can get by using just Debug.DrawLine()

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

11 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

Related Questions

changing gravity for one object. 5 Answers

Best way to throw object 0 Answers

How would I make an object disappear after a set amount of time? 3 Answers

Turret floats away and pushes everything away from it 2 Answers

jump script : 2D 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