I’m building a small fps, and sometimes you can shoot objects off the level and i want to make it so the object removes it’s self when it drops below the -1 Y axis.
i’ve tried to code this in my projectile class that i found in the fps tutorial.
so i tried the following line:
if (instantiatedProjectile.position.y <=0)
{
Destroy(instantiatedProjectile);
}
but it doesn’t seem to work at all. i’ve even tried gameOject as well.
We need to know more about instantiatedProjectile. What type is the object? Is it GameObject? Transform? Object? Also, if it is the allocated prefab that you use on the Instantiate method, it won’t work, you need to get the particular instance of that object in order to destroy it. Show us the entire code and we’ll help you.
using UnityEngine;
using System.Collections;
public class Shot : MonoBehaviour {
public Rigidbody projectile;
public float speed = 20;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate(projectile,
transform.position,
transform.rotation)
as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0,0,speed));
Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.root.collider );
//this is the part I'm having issues with. I want it if the player shoots off the level it destroys the projectile object.
if (instantiatedProjectile.position.y <=0)
{
Destroy(instantiatedProjectile.gameObject); // this didn't work either.
}
}
}
}
Maybe try reading the entire issue before posting a response?
instantiatedProjectile is of type RigidBody. I would suggest using instantiatedProjectile.transform.position.y instead. Also, print a log statement inside your conditional to check whether or not the code is being reached.
thing is you will loose the reference to your instantiated object, because you will instantiate them on mouse button. best thing to do is place a script on your bullet prefab, and make it check when its transform has this.position.y < 0, if true, destroy self or gameObject.
that way every instantiated bullet will check for it;s position and it will self destruct when is under 0 on y.
Seriously… you should learn to format your code before posting it, then you’d find out the error yourself by just looking at it.
Formated Code:
using UnityEngine;
using System.Collections;
public class Shot : MonoBehaviour {
public Rigidbody projectile;
public float speed = 20;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1")) {
Rigidbody instantiatedProjectile = Instantiate(projectile,
transform.position,
transform.rotation)
as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0,0,speed));
Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.root.collider );
//this is the part I'm having issues with. I want it if the player shoots off the level it destroys the projectile object.
if (instantiatedProjectile.position.y <=0) {
Destroy(instantiatedProjectile.gameObject); // this didn't work either.
}
}
}
}
As you can see, the “if(instantiatedProjectile.position.y <= 0)” is inside of the
if (Input.GetButtonDown(“Fire1”)) {
line, which means: This part of the code will only be executed ONLY ONCE when the user press the fire button. It won’t be called again until he press again!
There is a few things you need to do to fix this.
declare “Rigidbody instantiatedProjectile” as Class member
move the “if(instantiatedProjectile.position.y <= 0) { … }” block, outside of the "if (Input.GetButtonDown(“Fire1”)) { " block
I won’t post complete code, because you should learn it instead of copy and paste.
edit: @ar0nax: Right. That’s of course the “correct” way to do it OOP wise. But his attempt can also work, if the weapon of his choice only fires one shot at a time (like Angry Birds/Crush the Castle). But that’s not specified in the opening post
yes, Tseng, you are right, but in his code, i didn’t see anything about delay or making it impossible to shoot if a bullet is still present so i assumed that he wants to shoot as many bullets as he can.
PS: i failed to notice that he did the checking inside if(Input.GetMouseButtonDown(“Fire1”)) thing