- Home /
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;
}
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.
I didn't even know where to start, so I do not have an existing script. Sorry
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.
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!
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
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.