Jip1912
September 7, 2015, 8:02pm
1
Hi,
I got this problem that when I fill in coordinates at my array, the random spawn object doesn’t spawn at the coordinates I filled in.
GIF:
PIC:
I would normally use 3 elements so that a line spawns at 1 of the 3 possible lines.
I use this code for ‘‘Random Spawn’’ :
public Vector3[ ] positions;
void Start ()
{
int randomNumber = Random.Range (0, positions.Length);
transform.position = positions[randomNumber];
}
}
The code works, the only problem is that the item doesn’t spawn at the coordinates I filled in.
Thanks for helping.
Suddoha
September 7, 2015, 8:18pm
2
Jip1912:
Hi,
I got this problem that when I fill in coordinates at my array, the random spawn object doesn’t spawn at the coordinates I filled in.
GIF:
https://gyazo.com/945bd80510ae145ab54da6a529e60dd9
PIC:
https://gyazo.com/1ea0812b2dd95641cfa8cfebf9f9b425
I would normally use 3 elements so that a line spawns at 1 of the 3 possible lines.
I use this code for ‘‘Random Spawn’’ :
public Vector3[ ] positions;
void Start ()
{
int randomNumber = Random.Range (0, positions.Length);
transform.position = positions[randomNumber];
}
}
The code works, the only problem is that the item doesn’t spawn at the coordinates I filled in.
Thanks for helping.
Your object has a parent, which is obviously not at the world origin.
The position you see in the inspector is always the local position, i.e. relative to the parent, but transform.position always accesses the world position.
Solution is to either move your parent to the world origin for simplicity, or set transform.localPosition to the value you’ve got there.
Jip1912
September 7, 2015, 8:28pm
3
Suddoha:
Your object has a parent, which is obviously not at the world origin.
The position you see in the inspector is always the local position, i.e. relative to the parent, but transform.position always accesses the world position.
Solution is to either move your parent to the world origin for simplicity, or set transform.localPosition to the value you’ve got there.
Thank you so much! This really helped me a lot.