Spawn objects randomly but not close to player

0 favourites
  • 7 posts
From the Asset Store
Basic Rounded Vector Geometry Player Design with Glow for 3 player games
  • Hi,

    Sorry, might be a double post - asked this in the 'General' forum by mistake and didn't know how to delete my post.

    My question: How do I spawn objects randomly (within the game arena) while making sure that it won't spawn close to where my player is at the moment of spawning? Will this involve some scripting?

    (Also asking as I am curious what level of complexity will force me to learn some JavaScrip!)

    Many thanks for help!

  • Have a look at this example. Very simple. There are other ways you could do it as well.

    onedrive.live.com/redir

  • Thanks, will do! Need to update first, you seem to have used a recent beta release and I'm still on 2.60.

  • I used an overlap check with a buffer sprite to help you visualise what is happening but you can replace the use of the buffer sprite with a simple distance check which will be more efficient.

    Instead of "Spawner is overlapping buffer" use:

    distance(Spawner.X,Spawner.Y,Player.X,Player.Y)<whateverdistanceyouwant

    and

    Instead of "Spawner is not overlapping buffer" use:

    distance(Spawner.X,Spawner.Y,Player.X,Player.Y)>=whateverdistanceyouwant

  • If your spawning enemies or whatever around your player you could try something like this. Assign a new instance variable to your enemy called spawnAngle.

    Spawn a new enemy ontop of your player

    Set the spawnAngle variable of the enemy to: atan(random(0,1)/random(0,1))

    Then set the position of the enemy to:

    X: cos(self.spawnAngle)*(round(random(0,1))*2-1)*random(300,700)

    Y: sin(self.spawnAngle)*(round(random(0,1))*2-1)*random(300,700)

    Where 300 is the minimum distance and 700 is the maximum distance away from the player.

  • Thanks, there seem to be a few ways to do this. I will have to spend a bit more time to fully understand your approach, complete beginner here! :)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • If math functions scare you, you can use the method I posted in your other thread, it's essentially the same:

    construct.net/en/forum/construct-3/how-do-i-8/spawn-objects-randomly-away-164182

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)