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 Fadawar · Oct 08, 2015 at 10:33 PM · 2d2d game

Random spawn outside viewport - Wrap around screen

I am creating an asteroids like game, and I need it to randomly place asteroids out side the viewport and apply force to the rigidbody2d in the direction of the viewport. For one how would I do this and where should I put the script? Thank You!!

P.S. I have an asteroid prefab.

 bool isWrappingX = false;
     bool isWrappingY = false;
     bool isVisible = true;
     float transportcooldown;
 
     // Use this for initialization
     void Start () {
 
     }
 
 
     // Update is called once per frame
     void Update () {
         transportcooldown--;
 
         screenWrap ();
         var cam = Camera.main;
         var viewportPosition = cam.WorldToViewportPoint(transform.position);
 
 //        if (isVisible == true && viewportPosition.x > 1 || isVisible == true && viewportPosition.x < 0 ||
 //            isVisible == true && viewportPosition.y > 1 || isVisible == true && viewportPosition.y < 0) {
 //            isVisible = false;
 //        }
 
         if (viewportPosition.x > 1 || viewportPosition.x < 0 ||
             viewportPosition.y > 1 || viewportPosition.y < 0) {
             isVisible = false;
         } else if (isVisible == false) {
             isVisible = true;
         }
 
 
 
     }
 
     void screenWrap()
     {
         if (transportcooldown > 0) {
             return;
         }
 
         if(isVisible)
         {
             isWrappingX = false;
             isWrappingY = false;
             return;
         }
         if(isWrappingX && isWrappingY) {
             return;
         }
         var cam = Camera.main;
         var viewportPosition = cam.WorldToViewportPoint(transform.position);
         var newPosition = transform.position;
         if (!isWrappingX && (viewportPosition.x > 1.1 || viewportPosition.x <-0.1))
         {
             newPosition.x = -newPosition.x;
             isWrappingX = true;
 
         }
         if (!isWrappingY && (viewportPosition.y > 1.1 || viewportPosition.y <-0.1))
         {
             newPosition.y = -newPosition.y;
             isWrappingY = true;
 
         }
         transform.position = newPosition;
         transportcooldown = 20;
     }

Comment
Add comment · Show 4
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 Peysbubby · Oct 08, 2015 at 10:41 PM 0
Share

Do you have any script you are using at all? If you do would you please edit your post and add the code to it, so we could see it and help you easier.

avatar image Fadawar Peysbubby · Oct 08, 2015 at 11:15 PM 0
Share

I didn't even know where to start, so I do not have an existing script. Sorry

avatar image dkjunior · Oct 08, 2015 at 11:17 PM 0
Share

You can also take a look at Unity's Space Shooter tutorial - https://unity3d.com/learn/tutorials/projects/space-shooter-tutorial

It's doing this and many other things that may be useful in your game.

avatar image Fadawar dkjunior · Oct 09, 2015 at 12:21 AM 0
Share

Thank you for the link, i now have the asteroids spawn randomly but for some reason the asteroids don't screen wrap if they are spawned outside the viewport. The work great if spawned inside. What is causing this and how can i fix it thank You. The screen wrap script is above!

2 Replies

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

Answer by meat5000 · Oct 09, 2015 at 02:39 PM

screenwrap() doesnt seem to account for the

if(!isVisible) condition.

 Outside viewport?  ->   isVisible = false;

 screenwrap();
 
             ( isWrappingX = false;
              isWrappingY = false; )

 if (!isWrappingX && (viewportPosition.x > 1.1 || viewportPosition.x <-0.1))
      { //Wrapping false and outside viewport

BUT, you already moved it

 var newPosition = transform.position;

So

 ( isWrappingX = false;
   isWrappingY = false; ) //remains
Comment
Add comment · 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
0

Answer by brazmogu · Oct 09, 2015 at 09:12 AM

Look up the Instantiate function on how to create copies of an object in the scene, and you'll probably want to also check Coroutines for looping and spawning in parallel.

Rigidbody has functions to apply force and you can check out Vector3 or Vector2 functions to figure out the direction you need.

Finally, you can put the script on an empty GameObject, or even in your Camera for these cases where you have systems running without a specific game object to be attached to.

Comment
Add comment · 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

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

39 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnCollisionStay2D deactivates after a short amount of time 1 Answer

How would i calculate the size of a 2D area closed off by multiple colliders? 1 Answer

2D random shooting problem with velocity, 0 Answers

problem with rotation 2D 1 Answer

Getting a Tilemap to cast shadows 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