- Home /
How to delete this projectile after touching anything?? (C#)
Soo I've been using this script for a projectile who is shot everytime I press RMB. What I wanted to do is for the projectilet o destroy everytime it hit something, everything, I dont mind if they are walls, enemies, everything. The fact is that as many tutorials I see I dont seem to find any solution as the projectile just go trough the objects an not destroying them.
What I've been using for the Bullet (Projectile)
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
public float speed = 8f;
public float lifetime = 5f;
private float lifeTimer;
// Use this for initialization
void Start () {
lifeTimer = lifetime;
}
void OnCollisionEnter(Collision Collision)
{
Destroy(gameObject);
}
// Update is called once per frame
void Update () {
transform.position += transform.forward * speed * Time.deltaTime;
lifeTimer -= Time.deltaTime;
if (lifeTimer <=0f)
{
Destroy(gameObject);
}
}
}
And the gun script (If there's something making the bullet not to destroy on touch)
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public GameObject bulletPrefab;
public Camera PlayerCamera;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
GameObject bulletObject = Instantiate(bulletPrefab);
bulletObject.transform.position = PlayerCamera.transform.position + PlayerCamera.transform.forward;
bulletObject.transform.forward = PlayerCamera.transform.forward;
}
}
}
Any help is very much apreciated, Thanks ^^!
You might want to use rigidbody in changing your bullet's position ins$$anonymous$$d of changing its transform.position. $$anonymous$$oving object with transform.position ignore other objects even if they collided
Answer by sethuraj · Nov 27, 2017 at 08:49 AM
Add a collider and a RigidBody component to the bullet prefab object (if it doesn't already have one) and use OnCollisionEnter() to detect collisions.
Sample Code
public class Bullet:MonoBehaviour
{
//Function will be called when this object hits an object with a collider
void OnCollisionEnter(Collision collision)
{
//Destroy this gameobject
Destroy(gameObject);
}
}
Also you could set the bullet to destroy after a certain time
public class Bullet : MonoBehaviour
{
//Use this for initialization
void Start()
{
//Destroy the gameobject 5 seconds after creation
Destroy(gameObject, 5.0f);
}
}
I'm new but I think I have this right... For this to work all "walls, enemies, everything" must also have a colliders attached to them and "Is Trigger" must be selected in the properties of the collider in the bullet prefab.
Yes. All the other objects also require a collider. Also I updated the answer to destroy the bullet 5 seconds after instantiation.
Your answer
Follow this Question
Related Questions
Destroy temp object and make new object. 0 Answers
How to only delete one of two collided objects? 1 Answer
Destroy object on touch of object with specific class 3 Answers
Destorying an object when it hits the ground 1 Answer
The object of type 'Rigidbody' has been destroyed but you are still trying to access it. 2 Answers