- Home /
hide game object problems
hey i need help i have created a script but it does not work as i wanted it to. ihave an pointlight on the tip of my gun with an animation it goes from invisible to visible and than invisible. its for simulating the light of the gun firing when i getbutton("Fire1") im firing and i see the light flikkers thats good but then when i getbuttonUp ("Fire1") it disapears thats what i want but now it comes i want it to when i ("Fire1") again that its flikkering again.
can somebody please get this done for me becouse i can`t. dont know whats wrong Again.sorry for that.
function Update()
{
if( Input.GetButton( "Fire1" ) ) { gameObject.active = true; animation.Play("new gun fire light"); } if( Input.GetButton( "Fire1" ) ) { gameObject.active = false; animation.Play("new gun fire light"); } if( Input.GetButton( "Fire1" ) ) { gameObject.active = active = true; animation.Play("new gun fire light"); } } function Awake() { if( Input.GetButton( "Fire1" ) ) { gameObject.active= true; gameObject.active= false; animation.Play("new gun fire light"); } }
Answer by kennypu · Jan 05, 2011 at 08:42 PM
whats exactly not working? The code you're using doesn't work because you set active to true in update, then right on the next line, it becomes false. you have to set a timer, or have it delay some how so that you can actually see the light.
Answer by Ejlersen · Jan 05, 2011 at 09:01 PM
Your code doesn't work as you explain as stated by kennypu, but here is a quick example of how you could make it:
// Our light source public var lightSource : Light;
// Speed of the flicker public var flickerSpeed : int = 10;
// Lets store the flicker time, such that we don't have to calculate it more than once private var flickerTime : float = 0.0f; // Rest before next flicker private var flickerRestTime : float = 0.0f;
function Awake() { // Lets calculate the time between each flicker flickerTime = 1.0 / flickerSpeed; flickerRestTime = flickerTime; }
function Update() { // If we get button up, then lets reset it all to default if(Input.GetButtonUp("Fire1")) { lightSource.enabled = false; flickerRestTime = flickerTime; }
// If we are holding down the button, then
if(Input.GetButton("Fire1"))
{
// subtract the time since last frame
flickerRestTime -= Time.deltaTime;
// If the time is less than 0, then lets make the light turn on/off
if (flickerRestTime < 0.0f)
{
// Set the light source to the opposite of what it was before
lightSource.enabled = !lightSource.enabled;
// Reset the time
flickerRestTime = flickerTime;
}
}
}
yah tanks this was what i was looking for thanks only i have sett the speed from 10 t0 50 but tanks great work im greatful
Your answer
Follow this Question
Related Questions
game object hide 1 Answer
throw / fling object or prefab 1 Answer
Respawning script question. 1 Answer
Help With LookAt Target 1 Answer
End game then an Object is close to another object? 0 Answers