- Home /
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?
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.
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?
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 :)
I prefer doing in JS, but C# is just fine for me as well, clunk47! :)
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);
}
}
}
}
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.
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! :)
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.
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
Follow this Question
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