I am trying to get it so that enemies are spawned in a circle, (with a random angle and a radius that is within to numbers), where the circle’s center is the current position of the player object(this code is from a script that is attatched to the player). I have the instantiate code, but how can I make it so the circle is centered at the player’s current position (right now it appears to only be using the player’s position from when the scene started)?
edit: sorry, it looks like you only defined position once inside new Vector3. You never told script to update your calculation depending on player’s position. try something like player.position*Mathf.Cos()
hey, edited first post, but you replied faster than me…
instead of giving code, i’ll try to explain
Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object
See you are defining position : Vector3 only once with your Mathf.Cos values. Rotation however is working because you are using .identity.
When object moves, it’s new position is different, yet your script only cares about what you told it to do - only calculate given Mathf.Cos
to solve this, you simply need to take your Mathf, add it to current transform.position and put it into Update(). This way, every time transform.position values changes (player moves), Mathf.Cos will calculate coordinates accordingly to freshly updated position’s values
alright. Well it’s hard to predict how the enemies are spawned for you - automaticly by time or by press of a button.
But if you look at my signature at Critter AI, you can set horizontal sliders to amounts to spawn and when you press apply you will see that rats are spawned randomly in a circle around my gizmo (invisible object) or spawn point.
code i am using for that has nothing to do with Update() as it updates position when button is pressed
code is:
function Spawn () {
for (var i = 0; i < spawnerButton.numberOfRats; i++) {
var randomRange = Random.Range (randomRangeMin, randomRangeMax);
var angle = i * Mathf.PI * 2 / spawnerButton.numberOfRats;
var pos = transform.position + Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle)) * randomRange;
Instantiate(ratPrefab, pos, Quaternion.identity);
}
}
look at my code’s pos variable: var pos = transform.position + Vector3(Blablabla)… that is what you were missing
so if you compare them, you should easly figure this out
As for Update() i mentioned. If you want to spawn enemy around player automaticly depending on how much time has passed, you should then use Update() and place InvokeRepeating inside of it with timer defined. And point Invoke to your spawn function. Simple