Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 xylofiso · Aug 28, 2015 at 07:24 PM · cameradestroy2d-platformerprojectileinvisible

How to destroy a projectile when it leaves any side of the camera view on a 2D game?

Hello, I have tried searching the Questions for a resolution on this, but could only find one where the projectile would be destroyed upon exiting the camera on the right side of the screen while the firepoint is in the middle of the camera. I have been trying to get the projectile to be able to be destroyed, but I can't figure out the proper equation and have it destroying after it leaves the camera in about 2 seconds. I just don't want it being able to destroy any baddies that haven't been shown yet but are still in the stage. Below is what I'm working with code wise, and I'm wondering about trying to even devise a way to create a variable for the distance measured between the minimum x camera // maximum x camera verses the firepoint position, but I don't know if that is the only way to solve this, and even then I'm not too good with logical thinking like this.

This is the Update method in the script that controls the trajectory of the projectile:

 void Update() 
     {
         GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D>().velocity.y);
 
         Vector3 position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width,Screen.height,0f));
 
         Vector2 firePosition = (new Vector2 (player.GetComponent<Rigidbody2D> ().position.x, player.GetComponent<Rigidbody2D> ().position.y));
 
         if(transform.position.x > position.x || transform.position.x < -position.x - firePosition.x)
         {
             Destroy(gameObject);
         }
 
     }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Seb-1814 · Aug 30, 2015 at 03:37 PM

A simple solution would be to use OnBecameInvisible message.

 void OnBecameInvisible() {
     Destroy (gameObject);
 }

Hope this helps

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 xylofiso · Aug 30, 2015 at 07:10 PM 0
Share

Hmmm, this seems to be set to the #Scene window in Unity and not the actually camera bounds I have following the character. Thanks though.

avatar image Suddoha · Aug 30, 2015 at 07:27 PM 0
Share

@xylofiso Have you tried to build the project and test this again? That should actually work, one thing to note is that the objects count as visible as long as they can be seen by any camera, which includes the scene view when you're in the editor.

Ins$$anonymous$$d of building, you could also just turn away the scene camera so that the camera rendering your game is the only one that sees your projectile.

avatar image Seb-1814 · Aug 30, 2015 at 07:46 PM 0
Share

@xylofiso @Suddoha

the objects count as visible as long as they can be seen by any camera, which includes the scene view when you're in the editor.

Oh, I did not realize that. $$anonymous$$aybe then it would be more elegant to do calculate it by code.

avatar image Suddoha · Aug 30, 2015 at 08:03 PM 0
Share

@Seb1814 There's nothing wrong with your suggestion. It was just an explanation due to the comment that the function does not fullfill the needs, but it actually does. Just not while using the Unity editor (unless you turn the scene camera away from the object). It should work as expected as soon as the game is built.

Of course there are more elegant ways, like object pooling. But that could be improved later on.

avatar image xylofiso · Aug 30, 2015 at 08:14 PM 0
Share

It worked when I played the game maximized, didn't try this the first time. Thanks a bunch, quick and simple solution!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Android Game Projectiles becoming invisible 0 Answers

How to move camera with arrow keys while also using Cinemachine Follow + Look Ahead 0 Answers

Getting 2D projectiles that fire in the direction of the mouse position to work in unity 5? 0 Answers

Destroy clone when left Field of view? 1 Answer

Scene goes pink when I go left. 0 Answers


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