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 ghazaliIbrahim · Jan 18, 2013 at 12:58 PM · moveclickgui.button

move a button On click - rather than on game start

I have a button that moves in from right to its target position - script below. What I tried and couldn’t do is to: Move the button in when I click on a button (another button) rather than on start. How should I go about doing that?

 var target = 100.0;
 var move = 200;
 
 function Update () {
 
   move = Mathf.Lerp(target + 100, target , Time.time);
 
 }
 
  
 function OnGUI () {
 
      GUI.Button(Rect(move, 100.0, 100.0, 50.0), "Moving Button!");
       
   }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
 
 
  


Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Zarenityx · Jan 18, 2013 at 03:33 PM

Assuming you want to keep your move calculations in your Update() function, try this:

 var target = 100.0;
 var move = 200;
 private var moving : boolean = false;
 private var clicktime : float;
 
 function Update () {
 if(moving){
   move = Mathf.Lerp(target + 100, target , Time.time-clicktime);
 }
 }
 
 
 function OnGUI () {
 if(GUI.Button(Rect(//where you want the other button), "Make the button move!") && !moving){
      clicktime = time.time;
      moving = true;
 }
      GUI.Button(Rect(move, 100.0, 100.0, 50.0), "Moving Button!");
 
   }

You will need to change the '//where you want the other button' to a real rect, of course.

Comment
Add comment · Show 3 · 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 ghazaliIbrahim · Jan 18, 2013 at 04:18 PM 0
Share

I wish it worked - it just jumps to the end position. the lerp still calculates the distance if i click the button or not. I mean - if you click the button immediately after you hit play , the button moves. But if you click the button after waiting for a second the button it jumps strait to the end. and i don't $$anonymous$$d if it not in the update() function as long as it works. tnx

avatar image Zarenityx · Jan 18, 2013 at 04:37 PM 0
Share

Try it now (I edited it). I think the problem was this: When you press the button immediately, Time.time still is 0. But afterwards, Time.time>0, and therefore farther ahead in the Lerp. Here, I found the time when you pressed the button, and subtracted it from the current time to get the time since it was clicked. I also added "&& !moving". This keeps it from jumping back to the beginning again after you push it.

avatar image ghazaliIbrahim · Jan 18, 2013 at 04:46 PM 0
Share

Nice! it works like a charm! i wouldn't have come up with that :) Thank you zare' .I appreciate it a lot!

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

10 People are following this question.

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

Related Questions

How Can I write a Click To Move Script? 2 Answers

Click to move on a sphere (planetary gravity) 0 Answers

How do I move objects in one executed turn? 1 Answer

How to make an object move towards the mouse pointer on click? [2D] 2 Answers

I get errorsfor my Click to Move Script 0 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