Saturday, January 31, 2015
How to Prevent Your Actors From Spawning On Top of Each Other in UE4 Using Sphere Trace for Objects
So in this tutorial we will take a look at how to prevent actors from spawning on top of each other by using a Sphere Trace for Objects. The Trace for Objects set of functions are useful for many applications, but in this instance we will be using it as a work-around for the either defective or misleading "Spawn Even If Colliding" boolean on the SpawnActor function.
Created for UE4 v4.6.1
For this particular example, we used a modified method described in this post on the UE4 answer hub.
To set up the trace, you can look at the following image:
Initially, the Trace and the Actors would spawn in different locations. This was because we were feeding the Trace with coordinates that updated asynchronously with the spawned actor. To fix this, we set the stored vector coordinates for the next spawned actor first, and then used that vector for the trace and spawn transform. Since the stored vector only updated when a balloon was ready to be created, that vector was locked in the Stored Vector variable until the next balloon spawn.
The parameters for the Sphere Trace are pretty straight forward. The start and end vectors of the trace are the path that the sphere follows (which cannot be the same, so if you wanted to just check a sphere about a point then use a very small start and end vector difference, say +/-1). The object types are the objects that will set the return value to true if the sphere detects a collision. These values are pretty general, and you need to have an array (a list basically) of the types of actors/objects that will detect and object. Also, there is a VERY useful debug tool that allows you to visually monitor the traces if you so choose.
For the output, we used a branch with a boolean value. If the sphere makes a detection, then that actor never gets created, since the Return Value is set to TRUE. If there is no object detection, then the Return Value is set to false, passing the input to the Spawn Actor.
There may be a better way of doing this, but so far this seems to be the simplest way to spawn many objects without them spawning on top of each other. This could be useful in other applications, say spawning random projectiles/obstacles in a top-down flyer, or random troop spawn locations in an area for a tug-of-war or tower defense map.
Hopefully this tutorial helps you guys and saves you some time.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment