- Home /
Position problem, Make my own Button
I am trying to create my own big red button. Maybe there's a better way to do it, but this is what I came up with so far. It's supposed to be that when I click it moves down and when I release it moves back up. And it does some times, other times it starts moving up randomly. Any one know why?
var button: Transform;
var speed = 5;
function OnMouseDown(){
button.position -= button.position.up * speed * Time.deltaTime;
}
function OnMouseUp(){
button.position += button.position.up * speed * Time.deltaTime;
Debug.Log(button.position);
}
What exactly do you mean by "moves down" and "moves back up?"
I've made this script:
var speed = 5;
function On$$anonymous$$ouseDown(){
transform.position -= transform.position.up * speed * Time.deltaTime;
}
function On$$anonymous$$ouseUp(){
transform.position += transform.position.up * speed * Time.deltaTime;
}
which is almost identical to yours. I attached it to a cube and what it does is move the cube a tiny bit down when clicked and back up when released. $$anonymous$$ind of like a button :P Is this what you have in $$anonymous$$d?
If so, why do you need the speed variable? And if not, please explain more clearly how you want the button to behave =)
Also do you really want a 3D cylinder to be the button, or do you want a GUI one, like in the first answer =)
This should work, however you shouldn't use Time.deltaTime since it's a one time event and nothing that happens over multiple frames. Rena$$anonymous$$g "speed" to something like pushedDepth
makes a bit more sense since this variable just specifies how much the object is beeing moved.
One problem is of course when the button moves down, your mouse may leave the button area. The upevent should be checked in a coroutine or Update to be reliable and independent from the mouse position. If you don't move the button up reliably you can push it deeper and deeper ;)
Answer by venkspower · Apr 24, 2012 at 04:56 AM
Dude, use this in OnGUI().
Normally that would work, But I've used to cylinder objects to create the button so I'm not using GUI at all.
O$$anonymous$$. Do you have to use 2D textures as the button?
Im not using 2D textures at all, I'm using two cylinder gameobjects, It's really quite basic.
I guess, you have to set the positions of the cylinder gameObjects in the script. Sometimes, the positions you set in the inspector will not be the same, in Unity.
But why have you defined the button as transform here?
Answer by easilyBaffled · May 13, 2012 at 10:16 PM
Lerp worked! Here's the working code.
var button: Transform;
var confetti: GameObject;
function OnMouseDown(){
transform.position = Vector3.Lerp(Vector3(0,1.650255,0), Vector3(0,1.400255,0), Time.time);
yield WaitForSeconds(0.1);
transform.position = Vector3.Lerp(button.position, Vector3(0,1.650255,0), Time.time);
var fun = Instantiate(confetti, button.position, Quaternion.Euler(-90, 0, 0));
yield WaitForSeconds(4);
Destroy(fun);
}
It doesn't make much sense to use Lerp in this case. Time.time is the time since startup. Lerp requires a "t" value between 0.0 and 1.0 but Time.time is growing endless so it's always clamped to 1.0 (except for the first second in your game).
So it's essentional the same as:
//[...]
transform.position = Vector3(0,1.400255,0);
yield WaitForSeconds(0.1);
transform.position = Vector3(0,1.650255,0);
//[...]
Btw, it's absolute correct to define your button reference as Transform. If it was a GameObject reference you always need to get the Transform component (either with .transform or GetComponent) which is quite slow.
Always use reference types you want to use most of the time. If anothe object has a custom script of yours you need to access, the reference variable type should be your script type so you can directly access the script.
Your answer
Follow this Question
Related Questions
OnMouseOver Problem 3 Answers
C# MOUSE EVENT TOGGLE 1 Answer
Calling method once 2 Answers
How to: Click top Object of 2 overlapping objects (2D) 1 Answer
Input.MousePosition creating weird Z axis object translation 2 Answers