So I'm working on an on rails space shooter and I'm trying to figure out how to add in an asteroid field.
I'm not too sure where to start. I mean I know that adding loads of asteroids in would be a bad idea. So I was hoping to come up with one and then instantiate(?) it through script into different places and sizes and even add rotation and movement if possible.
Like I said I'm a bit unsure of how to start this so any help would be really handy.
Thanks guys :)
Hi, thx for this. it works so far but the asteroids stopping soon to rotate after being instantiated ! I'm also a beginner and i tried much to rotate them permanently but i havent achiefed it. I guess somehow in the update function. But how to change the script to make the asteroids rotate permanently ? Although this topic is 3 years old i hope i found some help thx previously !
Ego65 you just take any drag off the rigidbody. my asteroids rotate for ever and I have 0 Drag and 0 Angular Drag on the Rigidbody. BTW Do this on the prefab.
thx very much ! works perfectly now :) i would like to have different asteroids. what would be a "clean" way to achiefe this ?
Either : create diff ast-prefabs and instantiate them in this script like:
GameObject newAsteroid01=(GameObject)Instantiate(ast-Prefab01... GameObject newAsteroid02=(GameObject)Instantiate(ast-Prefab02...
Or : Attach this script to each kind of asteroid ?
I think this could be a thing which will influence the calculating perfomance of Unity
Yeah I just placed it on another prefab but this isn't the best way because you get ones overlapping I tried adding colliders to them but this got hilarious effects of them colliding voilently as the intiantate into the world and flinging off into space. If you create a way to put variations into one instantate let us know because I would love to use it. Btw the performance shouldn't be an issue if you use the LOD system. Make sure that the LOD3 is a billboard that is always facing the camera.
Answer by syclamoth
Feb 19, 2012 at 01:29 AM
Well, given a simple sphere prefab, you could try something like this-
for(int i = 0; i < 100; ++i)
GameObject newAsteroid = (GameObject)Instantiate(asteroidPrefab, Random.insideUnitSphere * fieldRadius, Random.rotation);
float size = Random.Range(0.3f, 2);
newAsteroid.transform.localScale = Vector3.one * size;
// if the asteroid has a rigidbody...
newAsteroid.rigidbody.velocity = Random.insideUnitSphere * movementSpeed;
newAsteroid.rigidbody.angularVelocity = Random.insideUnitSphere * rotationSpeed;
This should spit out a bunch of asteroids for you!
That seems like it'd work but I'm a bit unsure on how to set it up. I'm quite the noob to unity. I've created a prefab called Asteroid, and I've got a script file called asteroid field. Would you mind explain how to set it up a little more please??
Thank you :)
Well, obviously I haven't provided you with an entire script. I'll give you snippets like this, but I'm not that keen on people copy-pasting my code without understanding it. For starters, make a C# script file (because that's what languge this is in), and give it the public variables:
public GameObject asteroidPrefab;
public float fieldRadius;
public float size;
public float movementSpeed;
public float rotationSpeed;
This will allow you to configure it in the inspector. If you want to use the 'movement' parts, you'll also have to give your asteroids rigidbody components. If it's set in space, you should also disable gravity on them.
Thanks, for your help. I'm pretty sure I get the code, It's just tweaking it to do exactly what I need it to which is the challenging part...
In terms of placement of the Asteroid, I tried messing around with the placement
"GameObject newAsteroid = (GameObject)Instantiate(asteroidPrefab, Vector3(Random.Range(500,1500), Random.Range(-100, 200), Random.Range(50, 500)), Random.rotation);" but this throws up an error saying "Expression denotes a type, where a variable, value or method group is expected. Not too sure what that means... but I guess you can kinda see what I was trying to do with that code...
Also I was unsure where exactly to place the code that creates the asteroid. I've placed it in the start function and the update but they both cause keep making loads of asteroids and slow down unity til it crashes...
Any ideas on what I'm doing wrong??
You need to have the 'new' keyword before 'Vector3(value value value)'. There's a reason why I was using Random.insideUnitSphere- unless you want them spawning inside a box, not a ball. Don't put it in update, it'll just keep spawning asteroids. Put it in start- the entire point of the
for(int i = 0; i < 100; ++i)
line is that it executes that code exactly 100 times.
Do you know what the difference between a type and a value is?
Sorted it now! I think I was thinking into it too much lol
I'm not entirely sure of the difference, I'm a little rubbish with the actually words for coding stuff lol
Answer by Ego65
May 09, 2015 at 06:30 AM
thx craigb ! got it !
now that i wanted to use it in a new scene i realized that i can't set the ast.-field at a wanted position. seems it always spreads the asteroids from 0,0,0. i attached this script to empty game object and placed it at 0,0,700. so i thought it would spit out the ast. from this point. but it doesn't ! i tried to work with a public transform and instantiate the prefab there but whatever i try i get errors.
would someone help me on how to set the ast.-field to the coords of the empty game object please ?
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
8 People are following this question.
A quick way to find the root parent?
How i turn on/off a Object with GetKeyDown?
Multidimensional array of objects in C#
Instantiating an object returns null