• 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 Joseph-Ferano · Jun 04, 2011 at 03:32 PM · instantiatepositionvector3translateasteroids

Position an instantiated prefab relative to its translation vector

First time using Unity Answers but so far it has been really useful. I am currently doing an Asteroids Clone for Unity3D as my first learning project and so far it's going great. I had originally made Asteroids with Flash and now I am just figuring out how to do things the Unity way. Let me just say, Unity is awesome! What would take me 20-30 lines of code in Flash takes me 4 lines of code in Unity. Simply brilliant.

In my Flash version though, I came up with a way to have my asteroids spawn off of the screen and depending on the direction they were heading towards, they would spawn either top, bottom, left, or right. The reason I was doing this was so because I wanted the Asteroids to Destroy themselves once they left the screen again. You can imagine that if I just spawn them randomly without taking into account the direction they are heading to, at least 50% of them will be spawned and just Destroy immediately. Not efficient at all.

The way I achieved this in Flash was fairly simple, here's some pseudo code;

if (Asteroid.direction.x > 0 && Asteroid.direction.y > 0) spawnLeftSide(); //Because this is heading towards the top right if (Asteroid.direction.x < 0 && Asteroid.direction.y < 0) spawnRightSide(); //This one is going the opposite direction

So is there any fancy way of doing this in unity?

Another alternative is I could do it the other way around and first establish where they spawn, then give them the Vector. I can Clamp the values so as to make sure that they have a Vector that will direct them to the main view, instead of towards the out of bounds area.

While I currently have two ways of doing this, I would love to hear of any Unity specific ways to achieve this or if there are any more sophisticated algorithms out there, that would be really cool.

Thanks a bunch!

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 Peter G · Jun 04, 2011 at 03:54 PM 0
Share

Your way sounds about as good as any other. I'm sure there are similar variations but I can't think of "any fancy way" to do this in Unity.

avatar image Joseph-Ferano · Jun 04, 2011 at 04:09 PM 0
Share

Yup, I ask because I was trying to manually do the trig to get my Ship moving, when I found out there's a AddForce method which instantly takes care of that. Just wondering if there is anything similar.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Jun 04, 2011 at 04:07 PM

If I was doing something like this, I would probably create random position and direction, then mirror the wrong directions - something like this pseudo-code:

 if (position.x too close to the left side && direction.x < 0) {
   direction.x = -direction.x
 }


and only after x and y directions were right I would instantiate the asteroid. This is much more a matter of logic than an engine-specific question - but you're right: Unity is awesome! Flash is good for some simple applications, but a real game needs a real game engine - and Unity does the job!

Comment
Add comment · Show 1 · 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 Joseph-Ferano · Jun 04, 2011 at 04:11 PM 0
Share

Thanks for the idea. It's not bad at all and would save me the trouble of actually destroying the asteroids.

Yeah, I was also trying to get into the Unreal Engine but man, that is so daunting. I love how in Unity I can just get in there and start scripting away.

avatar image
0

Answer by Joseph-Ferano · Jun 05, 2011 at 03:02 AM

Well, if anyone is interested, this is what I came up with;

 //Spawn Position
     float spawnPadding = 2;
     float spawnRangeX = Random.Range(boundChk.minWrap.x, boundChk.maxWrap.x);
     float spawnRangeZ = Random.Range(boundChk.minWrap.z, boundChk.maxWrap.z);
     Vector3 quadrantN = new Vector3(spawnRangeX, 1, boundChk.maxWrap.z + spawnPadding);
     Vector3 quadrantE = new Vector3(boundChk.maxWrap.x + spawnPadding, 1, spawnRangeZ);
     Vector3 quadrantS = quadrantN * -1;
     Vector3 quadrantW = quadrantE * -1;
     float asteroidAngle = Mathf.Atan2(randomDirection.x, randomDirection.z) * Mathf.Rad2Deg;
     
     if (asteroidAngle > 45 && asteroidAngle < 135) {
         transform.position = quadrantW;
         print(asteroidAngle + " to the West");
     } else if (asteroidAngle >= 135 || asteroidAngle < -135) {
         transform.position = quadrantN;
         print(asteroidAngle + " to the North");
     } else if (asteroidAngle >= -135 && asteroidAngle <= -45) {
         transform.position = quadrantE;
         print(asteroidAngle + " to the East");
     } else if (asteroidAngle > -45 && asteroidAngle <= 45) {
         transform.position = quadrantS;
         print(asteroidAngle + " to the South");
     }

I spent a lot of time because I tried calculating the angle between vectors, should've done it like I did in Flash and used Atan2. Would've saved myself a good 45 minutes.

Code is pretty straight forward; Set up quadrants which act as spawn points. They are all outside the visible map. I use a BoundaryCheck class that allows me to encapsulate all of the boundary checking. minWrap and maxWrap just return the edges of the screen using ScreenToWorldPoint. I then calculate the angle the Asteroid is heading to and depending on the angle, I use the if statements to spawn it at a quadrant where it makes sense given the trajectory. So if it's heading at a 3 o'clock direction, it spawns on the West Quadrant, allowing it a greater chance of colliding with the ship.

Hope this helps anyone.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Position + Vector3 doesn't return correct values 1 Answer

Network.Instantiate Spanws player 2 wrong way 2 Answers

Randomly position Instantiated GameObject's 1 Answer

UnityEngine.Component:get_transform() Error 1 Answer

Instantiating objects at position 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