How would I write a javascript for unity3d that will allow me to press a button at the GUI in a 3d game , and spawn an item at the nearest area that is not already occupied by another object ?
Thank you,
Noob
How would I write a javascript for unity3d that will allow me to press a button at the GUI in a 3d game , and spawn an item at the nearest area that is not already occupied by another object ?
Thank you,
Noob
Something like:
var itemTemplate:GameObject
var OnGUI() {
if(GUI.Button(Rect(0,0,100,100), "Spawn Item")) {
SpawnItem();
}
}
function SpawnItem() {
var spawnPoint:Vector3;
spawnPoint = FindClearSpace();
var item = Instantiate(itemTemplate, spawnPoint, Quaternion.identity)
}
function FindClearSpace():Vector3 {
//here you implement your "find clear space" logic
//basically you'll need to know your start point (you say "nearest area" - nearest to what?)
//then you'll use [Physics.RayCast][1] to test nearby points until you find something that *doesn't* return a hit
//there are a lot of different ways to do this, and the right solution depends on your game. But as a starting point you
//could do something like test at 2m intervals along the X or Z axis until you find some clear space
//then return the Vector3 for that spot.
}
Try to ask specific questions. This question is far too general (as you can see from gribbly's, good, answer) which part are you having trouble with?
– anon73820239