- Home /
how to recollect the power object after it generate the power for few seconds
my pick up script:
var CoconutCount : int = 0; private var speed = 20; private var moveDirection = Vector3.zero;
function Update(){ if(CoconutCount >= 4){
controller = GetComponent(CharacterController);
transform.eulerAngles.y += Input.GetAxis("Horizontal") ;
moveDirection = Vector3(0,0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
controller.Move(moveDirection * Time.deltaTime);
}
}
function OnTriggerEnter(collisionInfo : Collider){ if(collisionInfo.gameObject.tag == "Coconut"){ CoconutCount++; //audio.PlayOneShot(batteryCollect); Destroy(collisionInfo.gameObject); } }
my collect script:
var GUICoconut : GameObject; var CoconutCount : int;
var Coconut1tex : Texture2D; var Coconut2tex : Texture2D; var Coconut3tex : Texture2D; var Coconut4tex : Texture2D; var Coconut0tex : Texture2D;
function Start(){ //GUICoconut = GameObject.FindWithTag("CoconutGUI"); GUICoconut.guiTexture.texture = Coconut0tex; }
function Update(){ CoconutCount = this.GetComponent("PickUp").CoconutCount; if (CoconutCount == 1) { GUICoconut.guiTexture.texture = Coconut1tex; } else if (CoconutCount == 2) { GUICoconut.guiTexture.texture = Coconut2tex; } else if (CoconutCount == 3) { GUICoconut.guiTexture.texture = Coconut3tex; } else if (CoconutCount >= 4) { GUICoconut.guiTexture.texture = Coconut4tex; } }
You question is unclear and your code should be formatted as code so it is readable.
Answer by spinaljack · Aug 30, 2010 at 08:10 AM
Instead of using destroy use renderer.enabled = false to hide it and then after a few seconds re-enable it.
e.g.
function OnTriggerEnter(collisionInfo : Collider){
if(collisionInfo.gameObject.tag == "Coconut" && collisionInfo.gameObject.renderer.enabled){
CoconutCount++;
//audio.PlayOneShot(batteryCollect);
collisionInfo.gameObject.SendMessage("RendererOff");
}
}
On the item do this to turn it back on:
var offTime : float;
function Update(){ if(!renderer.enabled && Time.time-offTime > 10){ renderer.enabled = true; } }
function RendererOff(){ renderer.enabled = false; offTime = Time.time; }
actually what i wants to achieve is when the character collect four coconuts,his walking speed will generate faster for 5 seconds, the after 5 seconds, his speed will reduce back to normal unless he need to recollect the four coconuts again...and how am i going to deal with my GUI..back to the empty bar when the character is generating power..
thx alot!!
Just set coconut count to 0 after a few seconds of collecting the 4th coconut. Use the same time stamp method.