- Home /
randomise spawnpoints position
hey, I`m trying to make it so when turrets run out a spawn point spawns a turret at its position, then it randomizes a new position were the next one will spawn(the order in which that happens doesn't matter) for any wierd reason it spawns one, then moves to an other position(the same one each time) and stays there and spawns all the others right there. so here is the code I`m using.
#pragma strict
var player : String;
var ObjectToDeploy : GameObject;
var deploied = true;
var turretCount : float;
var range1 : Transform;
var range2 : Transform;
function Update () {
Debug.Log("turretCount:"+turretCount);
if (turretCount <= 0){
Deploy ();
}
}
function Deploy()
{
if (deploied){
Instantiate(ObjectToDeploy, transform.position, transform.rotation);
deploied = false;
transform.position.x = Random.Range(range1.position.x, range2.position.x);
transform.position.x = Random.Range(range1.position.y, range2.position.y);
}
}
function TurretCountAdd () {
turretCount += 1;
deploied = true;
}
function TurretCountReduce () {
turretCount -= 1;
}
Thank you all and sorry for any spelling mistakes.
Answer by phodges · Oct 27, 2012 at 02:49 PM
In the code that you have posted, you are randomly setting the 'x' component of your deployment point's position twice. I expect you want to update x and z. Since the second random selection is based on the 'y' component of your two reference points, I'm also willing to bet that the reason you see no change in position after the first placement is because they are located at the same height.
As mentioned in another answer, your deployment point is hopping around the map and turrets being placed at its location. Is that really what you want? I suppose it could be useful to see where the next turret will appear, before it spawns, but there are other ways to do that. You want some code? Here's some code:
function Deploy()
{
if (deploied){
Instantiate(ObjectToDeploy, transform.position, transform.rotation);
deploied = false;
transform.position.x = Random.Range(range1.position.x, range2.position.x);
transform.position.z = Random.Range(range1.position.z, range2.position.z);
}
}
It may be his intention, and I could just be misunderstanding that, but, this code still moves the current object, and not the newly instantiated objects position.
The code I posted will get that job done with only 2 lines.
Does this code do what you wanted? As mentioned, hopping your spawning object around the map is a bit strange. I can delete this answer to keep things clean if not.
Answer by Montraydavis · Oct 27, 2012 at 02:49 PM
Because you are actually not changing the position of the newly instantiated object, but, the old one. Also, You might want to randomize before initiation.
Try this...
function Deploy()
{
var RandomVec3 = Vector3 ( Random.Range ( x, x ), transform.position.y, transform.position.z ) ;
if (deploied){
Instantiate(ObjectToDeploy, RandomVec3, transform.rotation);
deploied = false;
}
}
function TurretCountAdd () {
turretCount += 1;
deploied = true;
}
function TurretCountReduce () {
turretCount -= 1;
}