I would like to know if there’s a way for the First person controller to detect if there’s an instantiated object that it just spawned in front of him so he doesn’t spawn another overlapping the first one.
var yieldseconds = 5;
var mainmenuSkin : GUISkin;
var irrigation : Transform;
var irrigation2 : Transform;
var irrigation3 : Transform;
static var hitpass : Vector3;
function OnTriggerEnter(other : Collider)
{
print("Im in a collider!");
}
function OnGUI ()
{
GUI.skin = mainmenuSkin;
var ScreenX : int = 10;
var ScreenY : int = 600;
var areaWidth : int = 350;
var areaHeight : int = 100;
GUILayout.BeginArea (Rect (ScreenX, ScreenY, areaWidth, areaHeight));
GUILayout.BeginHorizontal();
if(GUILayout.Button ("Irrigation"))
{
irrigation1();
}
if(GUILayout.Button ("Exit Game"))
{
Application.Quit();
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
function irrigation1()
{
var clone : Transform;
var clone2 : Transform;
var clone3 : Transform;
var ground = transform.TransformDirection(-Vector3.up);
var hit : RaycastHit;
if(Physics.Raycast(transform.position, ground, hit, 0.1))
{
hitpass = hit.normal;
clone = Instantiate(irrigation, transform.position, Quaternion.identity);
yield WaitForSeconds (yieldseconds);
clone2 = Instantiate(irrigation2, clone.transform.position, clone.transform.rotation);
Destroy(clone.gameObject);
yield WaitForSeconds (yieldseconds);
clone3 = Instantiate(irrigation3, clone2.transform.position, clone2.transform.rotation);
Destroy(clone2.gameObject);
}
return;
}
This is what i have on the gui so far. Thanks for the help
you could do a distance check for the clone from the players point in space, or you could set a timer, so you dont spawn more than one clone pr second or minute,
you could do a:
function IsCloneNearMe(): boolean {
result = true;
var distance = Vector3.Distance(Clone.transform.position, transform.position);
if(distance >= dist) {
result = false;
}
}
or something like that, did not test the code though, you get the idea, and via the if(GUI.Button) function you instantiat an object more than once if the interval of the hold is to long, or so i believe.
Assets/custom scripts/Gui/guitest1.js(44,22): BCE0020: An instance of type ‘UnityEngine.Component’ is required to access non static member ‘transform’.
Assets/custom scripts/Gui/guitest1.js(47,28): BCE0020: An instance of type ‘UnityEngine.Component’ is required to access non static member ‘transform’.
Assets/custom scripts/Gui/guitest1.js(53,49): BCE0020: An instance of type ‘UnityEngine.Component’ is required to access non static member ‘transform’.
I did some more gui searching and retweaked my gui script a bit. I still havn’t found a way to check see if a clone is already on the ground so it doesn’t overlap.
Here’s the new script along with the Raycasting to make sure the first instantiated object is on the ground. (terrain)
var yieldseconds = 5;
var mainmenuSkin : GUISkin;
var irrigation : Transform;
var irrigation2 : Transform;
var irrigation3 : Transform;
var dist = 1;
var hitpass : Vector3;
var windowsize = Rect (20, 20, 120, 50);
function OnGUI ()
{
windowsize = GUILayout.Window (0, windowsize, WindowFunction, "Main Menu");
}
function irrigation1()
{
var clone : Transform;
var clone2 : Transform;
var clone3 : Transform;
var hit : RaycastHit;
var ground = transform.TransformDirection(-Vector3.up);
if(Physics.Raycast(transform.position, ground, hit, 0.1))
{
hitpass = hit.normal;
clone = Instantiate(irrigation, transform.position, Quaternion.identity);
yield WaitForSeconds (yieldseconds);
clone2 = Instantiate(irrigation2, clone.transform.position, clone.transform.rotation);
Destroy(clone.gameObject);
yield WaitForSeconds (yieldseconds);
clone3 = Instantiate(irrigation3, clone2.transform.position, clone2.transform.rotation);
Destroy(clone2.gameObject);
}
return;
}
function WindowFunction (windowID : int) {
if(GUILayout.Button ("Irrigation"))
{
irrigation1();
}
if(GUILayout.Button ("Exit Game"))
{
Application.Quit();
}
GUI.DragWindow (Rect (0,0, 10000, 20));
}