Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by shahinexir · Aug 11, 2012 at 01:04 PM · 2dinstantiateplayerdestroyprojectile

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 11, 2012 at 01:11 PM 1
Share

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.

avatar image shahinexir · Aug 11, 2012 at 02:12 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shahinexir · Aug 11, 2012 at 02:25 PM 0
Share

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

avatar image Bluk · Aug 11, 2012 at 05:43 PM 0
Share

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.

avatar image shahinexir · Aug 12, 2012 at 07:14 AM 0
Share

oh yes sure man ;) sorry and tnx again . im new here so forgive me :)

avatar image AnXgotta · Aug 12, 2012 at 04:13 PM 0
Share

Just to add some helpful functions. If you want to destroy the object when it leaves the view you could use the functions:

OnBecameInvisible()

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.OnBecameInvisible.html

avatar image shahinexir · Aug 13, 2012 at 02:19 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges