- Home /
Spawn enemy at an offset
var player: GameObject;
var enemy : Transform;
private var timer: float;
var seenDistance = 40;
var rotationSpeed = 100;
var moveSpeed = 5;
function Awake()
{
timer = Time.time + 10;
}
function Start ()
{
}
function Update ()
{
var distance = Vector3.Distance(transform.position, player.transform.position);
if(distance < seenDistance)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(player.transform.position - transform.position),rotationSpeed * Time.deltaTime);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
if (timer < Time.time)
{
Instantiate(enemy, transform.position, transform.rotation);
timer = Time.time + 10;
}
}
}
This is the code for my enemy, i have it set up so that when the player gets within the distance of an enemy, it will start to spawn more and more enemies. This works the way i want.
What i need help with is the spawn function creates the enemy on top of the other enemy and as a result, the spawned enemy gets launched into the air for a short time.
Is there a way i can spawn the enemy to a slight offset of the enemy it's being created from?
Any replies appreciated.
Answer by Norcine · Nov 21, 2013 at 10:24 PM
The easiest way to do this is to add a simple offset to your class:
var offsetX = 5;
var offsetZ = 5;
function Update()
{
// your stuff here up to the timer check
if(timer < Time.time)
{
var offset = Vector3(Random.Range(-offsetX, offsetX), 0, Random.Range(-offsetZ, offsetZ));
Instantiate(enemy, transform.position + offset, transform.rotation);
timer = Time.time + 10;
}
}
Answer by paulygons · Nov 21, 2013 at 10:21 PM
I think you need this: http://docs.unity3d.com/Documentation/ScriptReference/Random-insideUnitCircle.html
I hate it when I spend an hour writing a page long function that could have been done in one line!
Your answer
Follow this Question
Related Questions
How to have a cloned/duplicated enemy react the same way the original does? 1 Answer
Question about instantiate AI 0 Answers
When I pause the game the clone does not stop 0 Answers
Why I have a cloned prefab when spawning gameObjects? 0 Answers
ragdoll spawn flies up into air instead of staying still 1 Answer