• 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
-1
Question by Zekt · Jun 13, 2013 at 04:38 AM · buttonifelse

Button if else statement?

Hey Unity Answers community,

If I was to make an else statement for a singular button, were if you hit the non-desired key it would go back 2 points on the axis rather than +2 on the axis, how would I go about doing that?

And also, how can I make it so the desired button can only be hitten once? rather than be spammed.

Here's the code for one of my buttons:

 function Update () {
 
 if(buttons[0])
      {
      if(Input.GetKeyDown("q"))
 { 
 other.transform.Translate(Vector3(0.5,0,0));                  
 }
      } 
      

Thanks in advace.

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
-1

Answer by eddyvanleuten · Jun 13, 2013 at 06:44 AM

With this code your gameobject should move only once ( and dont forget the Time.delta! ):

     function Update()
         {
             if(Input.GetKeyDown("q") && keyPressed == false)
             {
                 transform.translate(Vector3(-2f,0,0)*Time.delta)
                 keyPressed = true;
             }
         }
         
     
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

15 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

Related Questions

How to finish my else if statement? 2 Answers

BCE0044 Error in the Start function 1 Answer

I am getting error cs1525 for '}' on line 11,17. How come? 1 Answer

problem with instance 1 Answer

Else and If 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges