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 TakMarche · Jun 15, 2013 at 07:12 AM · orthographicmovements

Handling Enemy Movement Within Camera View

I'm working on a game where I spawn the enemies just outside the camera view. At first I thought that having a Random.Range set would do the trick for moving them into camera view at a random distance but I noticed that if the max number is to high then it will move out of scene view and if it's to low it they would all be at a same distance. How can I make it so that I can spawn my enemies and allow them to move randomly across the scene within camera view?

P.S it a 2D game set on Orthographic

Edit:

"I'm pretty sure I'm envisioning to well in my mind I forgotten detail sorry. But let's put it this way, I got about maybe 20+ spawn points just setup just right outside the camera view. My script would randomly select one of those points and spawn an enemy. The enemy then moves an X amount of distance based on my min and max number which are set maybe 1 - 20 (so it travel a 1 - 20 distance) across the screen into view. My problem is that the width of the screen is different and so is the height so if it spawns at the bottom of the screen and travels 20 distance up it will then leave the camera view but if it spawn on the left the 20 distance traveled will keep it inside the camera view. Hopefully this make more sense ><"

Comment
Add comment · Show 3
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 SinisterRainbow · Jun 15, 2013 at 08:10 AM 0
Share

Not enough info. I'm sure your envisioning the game fine in your $$anonymous$$d but no one else can. :) What max number? same distance to what? BTW - $$anonymous$$ost games have spawn points at certain locations, and ins$$anonymous$$d when you scroll out of view u can spawn new ones. So if your board is designed well, the enemies contribute to the challenge.

avatar image TakMarche · Jun 15, 2013 at 09:08 AM 0
Share

I'm pretty sure I'm envisioning to well in my $$anonymous$$d I forgotten detail sorry. But let's put it this way, I got about maybe 20+ spawn points just setup just right outside the camera view. $$anonymous$$y script would randomly select one of those points and spawn an enemy. The enemy then moves an X amount of distance based on my $$anonymous$$ and max number which are set maybe 1 - 20 (so it travel a 1 - 20 distance) across the screen into view. $$anonymous$$y problem is that the width of the screen is different and so is the height so if it spawns at the bottom of the screen and travels 20 distance up it will then leave the camera view but if it spawn on the left the 20 distance traveled will keep it inside the camera view. Hopefully this make more sense ><

avatar image SinisterRainbow · Jun 15, 2013 at 09:47 AM 0
Share

btw, can you post as much info as possible next time. there are millions of 2d games, enemy types, so helping you answer is the easy part, figuring out what you want answered is the hard part. :) I'm not sure i even answered your question correctly as I;m still guessing. But one thing for sure, is that you don't want to randomly move things into view, you want to apply forces.. All major game engines do this, and probably almost all games.

1 Reply

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

Answer by SinisterRainbow · Jun 15, 2013 at 09:42 AM

OK, so I sort of understand. Right now I'm envisioning say an Asteroids type game, where maybe you spawn rocks outside and have it move towards the camera view yes?

IF this is the case, then you want to just apply a force to the objects, rather than move them randomly. For example:

 GameObject asteroid;
 
 float zDistanceFromCamera = 10.0f;
 
 Vector3 vScreenMidPoint = Camera.main.ViewportToWorldPoint(0.5f,0.5f,zDistanceFromCamera);
 
 Vector3 distanceToMidpoint = asteroid.transform.position - vScreenMidPoint;
 
 Vector3 normalizedV = distanceToMidpoint.Normalize;
 
 asteroid.rigidbody.AddForce(iImpulse * normalizedV);

(it's in C#, and untested btw) - but basically if Asteroid is your game object and you want to move it into view, i set a direction for it's movement (this always just moves towards the center of the viewport, if your 2d game is in a 3d world, then it moves toward the midpoint at a depth of zDistanceFromCamera.

You can easily change this to randomize where in view the asteroid will go, it can go towards any of the 4 corners or in between by changing:

   Vector3 vScreenMidPoint = Camera.main.ViewportToWorldPoint(0.5f,0.5f,zDistanceFromCamera);//change this
 
 ///--- To this:
 
   Vector3 vViewportPoint = Camera.main.ViewportToWorldPoint(Random.Range(0,1),Random.Range(0,1),zDistanceFromCamera);
 
 //well, also i changed the variable name to illustrate it's not the midpoint any longer...
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

15 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

Related Questions

How do I keep an Ortho camera in a specific range when the ortho changes? 3 Answers

Is smooth movement possible in Unity with a fixed camera? 3 Answers

Orthographic camera size question 1 Answer

Calculating 2D camera bounds 8 Answers

Simple shader in orthographic mode 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