- Home /
i want to destroy a projectile after it passes a limit on x axis after instantiation(2d game)
hi guyssss. I really need some help. Im making a 2d game and my player can instantiate projectile. My camera is attached to the player, so that when player goes right or left the camera moves and follows it. Now i want a code that if the distance between projectile and player was for example > 5 in x axis then it destroys it self. I mean, if the projectile went out of player ( camera) sight then it destroy itself. Also consider that the player can move, so when he instantiates a projectile if he move in the same direction as projectile it will get closer to it after a while. So i want it to be destroyed if it passed a for example 5 meters after the instantiation. tnx alot
This is the last time i format your posts. For god's sake put a space behind a full stop or UA will recognise it as an URL.
Also things like "I really need some help" are unnecessary. Just ask your question and be as specific as possible. That doesn't mean you should post your whole script again and again (like you already did). If you have a succeeding question, just post a link to your first question and concentrate on the relevant parts.
alright man . so sorry . its ok ;) . why are u so angry!?
i was facing with new problems and also corrected some of them and took my answer thats why i posted them again , as the last results . and if u look better u wont see any repetetive code or anything like that here in this post ! im new here and didnt now about the full stop problem that you mentioned , tnx . and it wont happen again boss!!! i corrected this problem here that i asked by myself.
Answer by Bluk · Aug 11, 2012 at 01:52 PM
function Update() {
//the distance between player and projectile
if (Mathf.Abs(player.transform.x - projectile.transform.x) > 5)
Destroy(projectile);
//The distance traveled
else if (Mathf.Abs(projectile.GetOrigin().x - projectile.transform.x) > 5)
Destroy(projectile);
}
So just to say it: this script won't work as is. Don't try to copy paste it ;). UnityAnswers is not a place where people can code for you. Especially when you have the algorithmic in your question. The problem you seem to be having is basic unity programming, you might want to do some tutorials before getting right into coding your games. http://unity3d.com/support/resources/tutorials/
Try to understand the code i put up here and what you need to do/modify to make it work.
tnx alot man . apreciate for your help . yeah im new with unity scripting , but ive studied and watched this tutorials as well . actually i fixed it by myself a few $$anonymous$$utes ago but tnx again.
hers my code if u wanna see it : this is a part of the projectile script:
var distance = transform . position.x - GameObject.FindGameObjectWithTag("Player") . transform.position . x;
if ( distance >= 4 )
{
Destroy(this . gameObject);
}
now there is another problem : i have a big cube on the floor as an plane . and there are 2 other cubes as wall on top of that . and + player . all of them ( cubes and player ) have rigid body . i have is kinematic on and is trigger off on my plane cube.and have just a rigid body with no kinematic and is trigger on my player.and is trigger on , is kinematic off on my wall cube . so by setting all of this if i instantiate a projectile it will get destroyed after it hits the wall,and the collision works.but also i dont want my player pass through the walls . want them colide with each other, but it doesnt work , and it seems that they dont . tnx
You're welcome. If this question is answered to you, you can mark it answered. As for the other question, you have to create another question on unityAnswers ;) If your other question is related to this one, you can link it on the other question, but you should always ask one question at a time.
And remember to make your text more readable by formatting it (line wraps and stuff), because here it is really hard to read.
Cheers.
Bluk.
oh yes sure man ;) sorry and tnx again . im new here so forgive me :)
Just to add some helpful functions. If you want to destroy the object when it leaves the view you could use the functions:
OnBecameInvisible()
tnx anxgotta ;) i used a variable as distance and just calcolated the distance between player and my projectile and then put an if statement that if distance was greater than ... then destroy game object . and it works.but tnx again for your help . learned somthing new ;)
Your answer
Follow this Question
Related Questions
Firing only a single projectile at a time, then adding a second. 2 Answers
Player invisible on respawn 2 Answers
Getting an error message when destroying a instantiated prefab 1 Answer
2D Space Shooter Instantiating bullet question 1 Answer
Changing the sprite of an instantiated object through a collision box from another gameobject. 1 Answer