- Home /
wait for 3 seconds button??
im looking to script for a button.
when i press it it does somethink, but when i hold it for 3 seconds it does something else ??
Are you using unity's OnGUI function to make your buttons? or are you using like GUITextures, or 3D assets that you raycast too? Hard to help you with so little information :/ do you have you starting code at all?
Answer by psychentist · Apr 30, 2012 at 01:43 AM
Ok. Normally I'd tell you to learn your own scripting, but I love coding so much, I couldn't pass up this uber easy question. For a 3d asset such as a cube you want to click, here's the script.
private var i : int;
function OnMouseDown(){
StartCount();
}
function OnMouseUp(){
StopCount();
}
function StartCount(){
for (i=0;i>-1;i++){
yield WaitForSeconds(1);
}
}
function StopCount(){
if (i>3){
//Do your greater than 3 seconds of holding stuff here.
}
else{
//Do your less than 3 seconds stuff here.
}
i = 0;
}
As for a GUI button, It's quite the same, but you gotta declare the button's domain and such. See the scripting reference for everything else you need to know.
Answer by kolban · Apr 30, 2012 at 02:49 AM
For determining how long a mouse button is held down, I think the following code may work:
private var mouseDownStart:float;
private var hasFired:boolean = false;
function Update () {
if (Input.GetMouseButtonDown(0))
{
hasFired = false;
mouseDownStart = Time.time;
}
if (Input.GetMouseButton(0))
{
if (!hasFired && (Time.time - mouseDownStart) >= 3)
{
// 3 seconds has passed with the mouse down
hasFired=true;
// Do something
}
}
}
The way this works is to note the time the mouse button was first held down and then, while it is still down, see if it has been 3 seconds since the mouse button was first held down. The guard prevents re-fires after 3 seconds.
Answer by samz · Apr 30, 2012 at 07:57 AM
its using GUI textures...i forgot to say This is the script for the touch button copied from the SideScroll tutorial n this is what i had so far:
function Start() {
endTime = 0;
}
function Update() {
//::::::::::::::::::::::::: H I T ::::::::::::::::::::::::::::::
if (!hitTouchPad.IsFingerDown()) {
canHit = true;
}
if (canHit && hitTouchPad.IsFingerDown()) {
while( hitTouchPad.IsFingerDown() ){
if(endTime < 3){
endTime += Time.deltaTime;
}else{
ShowItems();
break;
}
}
if( endTime < 3 ){
hit = true;
canHit = false;
}
}
if ( hit ) {
hit = false;
//hit code
}
}
function ShowItems(){
//show other hit items
}
Your answer