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
19
Question by duckduckmoose · Oct 31, 2014 at 02:41 AM · uibuttoncharacter4.6hold

[4.6 b20] How to make a hold button for the new UI

Hello! This is not a question, but more of an understanding. For a few days i've been having problems with the OnPointerDown (it doesn't show in the event trigger) so here is a simple way to make a hold button which I used to make a character move right while holding down the button. (Yes! it works on mobile too)

First you want to add an event trigger to your button via add component, choose 'Add new' and select 'OnPointerDown' and hit the '+' sign and add 'OnPointerUp'. Next put your gameobject (I chose my player which has the tag 'Player') into the object box.

Now for the code

Create a C# script and put it into your player character. (I called mine 'rightmovement')

 using UnityEngine;
     using System.Collections;
     using UnityEngine.UI;
     using UnityEngine.EventSystems;
         
     public class rightmovement : MonoBehaviour, IEventSystemHandler {

     public GameObject character;
     public float maxSpeed = 10f;
     bool buttonHeld = false;

     public void pressed (BaseEventData eventData)
     {
       buttonHeld = true;
     }
     public void notpressed(BaseEventData eventData)
     {
       buttonHeld = false;
     }
     void Start () 
     {
         character = GameObject.FindWithTag ("Player");
     }
     public void Update()
     {
      if (buttonHeld)
       {
         // Do stuff
         Debug.Log ("click");
         // Move Character
         character.rigidbody2D.velocity = Vector3.right * maxSpeed;
       } 
      else
        Debug.Log ("not click");
       }
     }

Now if you click on the box next to your gameobject in the event trigger, you can choose your script and then the button!. make sure your pressed and not pressed voids are PUBLIC.

I am a complete noob to scripting, but this just happened and I thought I'd share! Enjoy! :)

[1]: /storage/temp/34554-script.png

script.png (239.7 kB)
Comment
Add comment · Show 13
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 aslthunder · Jan 15, 2015 at 05:26 AM 0
Share

Hey friend, your script is perfect to my necessity! Thanks so much!

avatar image Safemilk · Jan 25, 2015 at 12:52 AM 0
Share

Thanks a lot, sir! This really helped me out!

avatar image Zhinkk · Jan 25, 2015 at 10:15 PM 0
Share

Thanks so much for the quick tutorial, helped out a lot :)

avatar image Rhogar · Feb 03, 2015 at 06:05 AM 0
Share

They should have made something like this a lot more intuitive... Thanks heaps for the $$anonymous$$i-tutorial, it saved me lots of hassle. $$anonymous$$uch appreciated =]

avatar image alex-dacol · Apr 10, 2015 at 07:27 PM 0
Share

Nice Tutorial my friend ! Thanks !

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DiegoSLTS · Jul 02, 2015 at 01:20 PM

In the Event Trigger component you can't choose the "OnPointerDown" event, the one you want is "PointerDown", without the "On".

You can also implement some interfaces for the events you want instead of the more general IEventSystemHandler. There's IPointerDownHandler and IPointerUpHandler, which force the class to implement OnPointerDown and OnPointerUp functions. This functions are called without any setup in the inspector.

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

Answer by Khurshed Gulov · Aug 05, 2015 at 03:42 PM

Thanks author, but I think to little bit update this functionality by using boolean properties to set the value for movement variable (in this case for "moveleft"). All other part of work like suggested the author.

 using UnityEngine;
 using System.Collections;
 
 public class RepeatButton2 : MonoBehaviour {
     // Assign after from editor
     public GameObject player;
     public bool moveleft = false;
     public int speed = 5;
 
     public void Update ()
     {
     if(moveleft)
         {
             player.transform.Translate(Vector3.left * speed * Time.deltaTime);
         }
     }
 
     public bool MoveLeft
     {
         get { return moveleft; }
         set { moveleft = value; }
     }
 }

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

Answer by MrMarkusHD · Aug 21, 2017 at 10:43 AM

@duckduckmoose Thank you soooooo much... i was searching for a result so long... i made my player also move left by copying and pasting that script and changing some things... i made the player move using AddForce on the rigidbody and i made him also jump... so i have 3 buttons... THANK YOU... i wouldnt be able to make this game if i didnt find this answer... a BIG THANKS :D

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

Answer by unity_gHkPKBUqwxeRig · Oct 09, 2017 at 08:02 PM

Thankkks mannnn you the best, worked like a charm

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

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[4.6 UI] Possible to reference parameter of OnClick persistent event? 0 Answers

4.6 UI Check if a button and a in-game object are overlapping 0 Answers

Unity UI Disable Annoying Yellow Arrow 1 Answer

Move gameobject to button in new 4.6? 0 Answers

Button is not selectable when pressing the submit button even though it is highlighted. How do I fix that? 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