- Home /
Question by
Datester35 · Sep 16, 2014 at 05:29 AM ·
shootshooter
How do you make a collision based fps script that deletes the object 2 seconds after it is fired?
Here is my current code:
#pragma strict
var prefabBullet:Rigidbody;
var shootForce:float;
var shootPosition:Transform;
function Update()
{
if(Input.GetButtonDown("Jump"))
{
var instanceBullet = Instantiate(prefabBullet, transform.position, shootPosition.rotation);
instanceBullet.rigidbody.AddForce(shootPosition.right * shootForce);
}}
I would like to add something to this script that would delete the bullet that is shot after 2 seconds.
Comment
Answer by HarshadK · Sep 16, 2014 at 05:31 AM
Add the script below to your prefabBullet game object prefab.
#pragma strict
function Start()
{
Invoke("DestroyBullet", 2); // 2 is the time after which bullet is to be destroyed.
}
function DestroyBullet()
{
Delete(gameObject);
}
Answer by jjplay175 · Sep 16, 2014 at 04:33 PM
It is really easy to do in C# for people that want it
Public GameObject bulletObj;
void Start()
{
Destroy (bulletObj, 2);
}
Really simple
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Shoot Script Help 1 Answer
Making a FPS, my gun floats sideways in front of me? 1 Answer
Bullet not moving 1 Answer