Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Pejovski · Oct 22, 2015 at 02:46 PM · androidbuttontouchtouch controlshold

Hold touch button Unity Javascript

I have script for crouching when i press and hold S its crouching when i release the button the player gets in his normal size. Now i want to do the same thing with touch button but i cant figure how to do that, can some1 help?

  static var vScale = 1.0;
  private var tr: Transform;
  private var dist: float; // distance to ground
 
  function Start(){
      tr = transform;    
      var ch:CharacterController = GetComponent(CharacterController);
      dist = ch.height/2; // calculate distance to ground
  }
 
  function Update()
  {
 
      vScale = 1.0;s 
      if (Input.GetKey("s")){ // press s to crouch
          vScale = 0.5;
      }
      var ultScale = tr.localScale.y; // crouch/stand up smoothly 
      tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
      tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position
   }
       function OnGUI ()
     { 
 }   

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 Statement · Oct 22, 2015 at 03:12 PM 0
Share

Could you please clarify "But i have cant add touch button ins$$anonymous$$ how to make Hold touch button?"

Do you want to support mobile touch input? Or toggle crouch when tapping S? Or handle a response from a UI button click? Or something else?

avatar image Pejovski Statement · Oct 22, 2015 at 03:21 PM 0
Share

Oh i started typing 1 thing then another.. i want it to support mobile touch input i have the script i want and its good for me, when im holding S its crouching, I want to make the game to do the same thing with touch button. Btw i changed my text.

1 Reply

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

Answer by Statement · Oct 22, 2015 at 06:38 PM

Downloadable project (history, code)

Refactor your Update code to call functions Crouch and Stand. To make sure your script works like before, use Input.GetKeyDown and Input.GetKeyUp. (See code changes)

Then add a Button to your scene. Since my example has Crouch and Stand functions, I add an EventTrigger to the button and hook up Pointer Down and Pointer Up events to route to Crouch and Stand. The setup on the button look like this:

When you touch or press the mouse, the following happens.

Red arrow: EventSystem send events to button and is intercepted by EventTrigger.

Green dotted line: EventTrigger has references to the Crouch Example game object.
Edit: Ignore the green dotted line to the script. It just means that the script sits on that game object...

Blue arrow: EventTrigger routes Pointer Down and Pointer Up events to Crouch and Stand.

Comment
Add comment · Show 9 · 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 Pejovski · Oct 22, 2015 at 07:24 PM 0
Share

Tnx :), but how to connect this with my button?

avatar image Statement Pejovski · Oct 22, 2015 at 07:38 PM 0
Share

You mean a UI button?

If you want to get an event both when the user put their finger on and off the button, try adding an EventTrigger to the button and route the calls from those callbacks ins$$anonymous$$d.

avatar image Pejovski Statement · Oct 23, 2015 at 12:51 PM 0
Share

Tnx, but 1 more thing can rewrite the code with crouch function? please

Show more comments
avatar image Pejovski · Oct 24, 2015 at 01:45 AM 0
Share

Dude i love u this is very useful I cant express my gratitude :D <3

avatar image Statement Pejovski · Oct 24, 2015 at 02:08 AM 2
Share

Love you say? Glad you're content. :)

avatar image Pejovski Statement · Oct 24, 2015 at 11:41 AM 0
Share

1 more question for u sir :D this one is little off topic, this is my old test game http://i.imgur.com/jHIjEIq.png?1 and this is from the youtube tutorial http://i.imgur.com/w$$anonymous$$wW39z.png, why the view is different, we have the same code for camera, his camera has view on 3 sides of the platform while $$anonymous$$e has just 2. How can i fix this?

Show more comments
Show more comments

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

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

Related Questions

How to get touch's positon when I hold the button?? 2 Answers

Why is my multi touch not working? 1 Answer

Touch on 2D gameobject: should i use a GUI button? 1 Answer

UI button events not working on android 3 Answers

When The Screen Is Touched Jump 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