Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by easilyBaffled · Apr 24, 2012 at 04:54 AM · positiononmousedownonmouseup

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);
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ravenmore · May 13, 2012 at 09:39 AM 0
Share

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 =)

avatar image Bunny83 · May 13, 2012 at 11:40 AM 0
Share

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 ;)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by venkspower · Apr 24, 2012 at 04:56 AM

Dude, use this in OnGUI().

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image easilyBaffled · Apr 24, 2012 at 05:21 AM 0
Share

Normally that would work, But I've used to cylinder objects to create the button so I'm not using GUI at all.

avatar image venkspower · Apr 24, 2012 at 05:22 AM 0
Share

O$$anonymous$$. Do you have to use 2D textures as the button?

avatar image easilyBaffled · Apr 25, 2012 at 03:40 AM 0
Share

Im not using 2D textures at all, I'm using two cylinder gameobjects, It's really quite basic.

avatar image venkspower · Apr 25, 2012 at 04:33 AM 0
Share

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.

avatar image venkspower · Apr 25, 2012 at 04:34 AM 0
Share

But why have you defined the button as transform here?

Show more comments
avatar image
0

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);

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · May 14, 2012 at 09:46 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges