Force Actors Apart
daniel.robert.campbell
Member Posts: 251
I have an actor that is randomly spawned around the scene. The problem is that sometimes two of this actor are spawned within one another. I want to try and find a way to either force them apart, or ensure that the actors never spawn within a certain distance of each other. Anyone have any ideas? Any guidance would be very much appreciated.
Best Answer
-
MobileRocketGames Posts: 128Create two game attributes. Name them Spawn1 and Spawn2.
Open up the actor that holds your spawning behaviours. Create a new rule that happens when whatever signals your actors to spawn happens (in my example, a game attribute called SpawnLocation). Have it roll two random numbers and assign them to Spawn1 and Spawn2. Afterwards it needs to check if the locations are the same (game.CheckLocation). Then have it signal the spawning function (in this case game.SpawnActor).
Rule:
attribute game.SpawnLocation = true- change attribute game.Spawn1 = random(1,20)
- change attribute game.Spawn2 = random(1,20)
- change attribute game.CheckLocation = true
- change attribute game.SpawnLocation = false
Create a new rule with the "on event":
attribute game.CheckLocation = true
Then, place a new rule inside that rule with the "on event":
attribute game.Spawn1 ≠ game.Spawn2- change attribute game.SpawnActor = true
- change attribute game.CheckLocation = false
- change attribute game.SpawnLocation = true
- change attribute game.CheckLocation = false
Basically what this is doing is when check location is turned on, it checks if the two numbers you rolled are the same or not. If they are the same, it turns back on the random function and rolls two new numbers and keeps doing this until it gets two numbers that arent the same. When it gets two different numbers, it turns on the spawning function. All you need to do is tell the spawning actors that if spawn1 = 1 then spawn in this location, if it = 2 spawn in this location. And so on. This will be especially easy if you use a table to store those locations (this way you only need 1 spawn behaviour and you put the link to the coordinates right into the X and Y inputs of the spawn behaviour, using the game.Spawn1 and Spawn2 in your links to the table row). I've done something similiar (exactly the same) in a game we are working on.
There may be some slight adjustments needed as some of the rules may fire too quick and I'm not in front of my computer so i can't test it out.
But the logic is sound and it does work.
Awesome? maybe. Insightful? I think so
Answers
Then if they collide or spawn on top of each other they will push apart.
Darren.