- Home /
Click button to fire weapon.
So i have this code as my original code:
var projectile:Rigidbody;
var TorpedoSpeed:float = 0.0;
private var launchLocation:Vector3;
launchLocation = Vector3(transform.position.x,transform.position.y,transform.position.z);
function Update()
{
if (Input.GetButtonDown("Fire1"))
{
GetLaunchLocation();
var instantiatedProjectile:Rigidbody = Instantiate(projectile,launchLocation,transform.rotation);
instantiatedProjectile.transform.Rotate(Vector3(180,0,0));
instantiatedProjectile.velocity = transform.forward * TorpedoSpeed;
Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
}
}
function GetLaunchLocation()
{
launchLocation = Vector3(transform.position.x,transform.position.y,transform.position.z);
}
but i want to re-code it so that this is the Input/Trigger
GUI.Button (Rect (1200, 800, 24, 24), GUIContent (PulseIcon));
So i did this:
function Update()
{
if (GUI.Button (Rect (1200, 800, 24, 24), GUIContent (PulseIcon)))
GetLaunchLocation();
var instantiatedProjectile:Rigidbody = Instantiate(projectile,launchLocation,transform.rotation);
instantiatedProjectile.transform.Rotate(Vector3(180,0,0));
instantiatedProjectile.velocity = transform.forward * TorpedoSpeed;
Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
}
}
but it is lagging my game to death and just overall not working, could anyone assist me?
Comment
Best Answer
Answer by robertbu · Feb 28, 2013 at 05:05 AM
GUI calls need to be done in OnGUI() instead of in Update():
function OnGUI() {
if (GUI.Button (Rect (1200, 800, 24, 24), GUIContent (PulseIcon)))
GetLaunchLocation();
var instantiatedProjectile:Rigidbody = Instantiate(projectile,launchLocation,transform.rotation);
instantiatedProjectile.transform.Rotate(Vector3(180,0,0));
instantiatedProjectile.velocity = transform.forward * TorpedoSpeed;
Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
}
}
It is probably slow because it is sending you error messages to the console window.
Answer by SrNull · Feb 28, 2013 at 05:28 AM
GUI.Button must be inside the function called "OnGUI( )" not inside "Update()"
function OnGUI() {
if (GUI.Button (Rect (1200, 800, 24, 24), GUIContent (PulseIcon))) GetLaunchLocation();
var instantiatedProjectile:Rigidbody=Instantiate(projectile,launchLocation,transform.rotation);
instantiatedProjectile.transform.Rotate(Vector3(180,0,0));
instantiatedProjectile.velocity = transform.forward * TorpedoSpeed;
Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
}
}