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 gugu6897 · Jan 06, 2013 at 02:55 PM · guiinputbuttonaction

Button Input

I'm Brazilian and my english is basic.

I'm a beginner in Unity 3D and in Javascript.

I have a "GUI Button" on the screen. I want to make a action when this button is pressed. My project is a race game. When the player press this Button, the value of Vertical Input is '1'.

 if (GUI.Button(Rect(1000,500,100,30),"Acelerar"))
 {
     //ACTION
 
 }

How can I do it? Please, help me guys.

Comment
Add comment · Show 5
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 AlucardJay · Jan 06, 2013 at 05:28 PM 0
Share

I'm trying to understand the problem. So you have Input.GetAxis("Vertical") , but when you press a GUI button, you want this value to change ? Really need an example to understand.

Say Input.GetAxis("Vertical") is returning 1. But if you press GUI button, this becomes -1 (like putting a car into reverse gear) ?

Or do you mean when you press GUI button, set the Input.GetAxis("Vertical") to stay at 1 ?

avatar image gugu6897 · Jan 06, 2013 at 06:35 PM 0
Share

A want to 'convert' the Axis Inputs (Vertical and Horizontal) to display on the screen and compile for Android device. Then I need to put a button on the screen. I want to make a 'equal' between the button and the Vertical input, like the 'Equal' between Up Arrow and W.

avatar image AlucardJay · Jan 06, 2013 at 06:46 PM 0
Share

So the buttons are doing the same job as Input.GetAxis("Vertical") , so you have 2 sets of inputs that do the same thing ? eg

 Input.GetAxis("Vertical") (press W) to move forward
 GUI.Button("Forward") also to move forward

 Input.GetAxis("Vertical") (press S) to move backward
 GUI.Button("Backward") also to move backward

is this correct?

avatar image gugu6897 · Jan 06, 2013 at 08:04 PM 0
Share

Yes. It is correct. Can you help me?

avatar image gugu6897 · Jan 07, 2013 at 01:12 AM 0
Share

But I want something like a link. The button action is linked to a vertical input.

In other situation, can i just add the vertical or horizontal axis on a gui to display? Does the click do an action?

1 Reply

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

Answer by AlucardJay · Jan 07, 2013 at 07:41 AM

I actually learned a new command while working on this! If you want a GUI.Button to detect if it is being held down, need to use GUI.RepeatButton (I don't use the Unity GUI).

This script demonstrates one way to achieve input from 2 sources. First, store 2 references for the inputs, one for the GetAxis and one for the GUI.Buttons :

 private var vertInputGUI : float = 0.0;
 private var vertInputKey : float = 0.0;

then add these 2 together, and make the result a value between -1 and +1 :

 vertInputAll = Mathf.Clamp( vertInputKey + vertInputGUI, -1.0, 1.0 );

then just use this combined input value for your movement. Here is a test script. Create a new scene, create a cube, attach this script to the cube :

 #pragma strict
 
 public var speed : float = 5.0;
 
 private var vertInputGUI : float = 0.0;
 private var vertInputKey : float = 0.0;
 private var vertInputAll : float = 0.0;
 
 function Update() 
 {
     vertInputKey = Input.GetAxis( "Vertical" );
     
     vertInputAll = Mathf.Clamp( vertInputKey + vertInputGUI, -1.0, 1.0 );    
     
     transform.position += transform.forward * vertInputAll * speed * Time.deltaTime;
 }
 
 function OnGUI() 
 {
     if ( GUI.RepeatButton( Rect( 10, 10, 100, 35 ), "Forward" ) )
     {
         vertInputGUI = 1.0;
     }
     else if ( GUI.RepeatButton( Rect( 10, 55, 100, 35 ), "Backward" ) )
     {
         vertInputGUI = -1.0;
     }
     else
     {
         vertInputGUI = 0.0;
     }
 }


Unity Scripting References :

Clamp : http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Clamp.html

GUI.RepeatButton : http://docs.unity3d.com/Documentation/ScriptReference/GUI.RepeatButton.html

Comment
Add comment · Show 4 · 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 gugu6897 · Jan 07, 2013 at 10:36 AM 0
Share

Thank you very much, it works perfectly. I'm very grateful.

avatar image Fattie · Jan 07, 2013 at 11:01 AM 0
Share

hey @gugu, if it's done, just click the "TIC$$anonymous$$" button

(round circle near thumbs)

only you can do it

avatar image gugu6897 · Jan 07, 2013 at 11:09 AM 0
Share

Thx. It's done.

avatar image mazhar-ali · Jun 02, 2014 at 08:19 AM 0
Share

Thanks so much...

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

12 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

Related Questions

How do I make a "move pad" on the screen? (Mobile) 1 Answer

Making GuiButton trigger on ps3 controller input 0 Answers

How can I make GUI button send an input message when I press it? 0 Answers

how to create buttons using iphone look and feel?? 2 Answers

Clicking Trigger 1 Answer


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