Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 prafull2001 · Sep 10, 2017 at 11:11 PM · button2d gamebuttonsspace shooterbuttonstates

Moving Player Left/Right with Buttons

I am creating a 2D Space Invaders game and I want to be able to move the player's ship side to side using "Left"/"Right" buttons (UI) i.e. run 'this' method as long as 'this' button is pressed.

I have also tried adding my buttons to input manager but I don't think it worked.

I have already created and placed the buttons but the script I attached to them does not work at all, including the debug print statement:

 private PlayerController playerController;
 
        void Start(){
 
         playerController =     GameObject.Find("Player").GetComponent<PlayerController>();
 
     }
 
 
     void Update () {
 
         if (Input.GetButtonDown ("Right")) {
             print ("RIGHT CLICKED");
 
             playerController.transform.position += Vector3.right * 15.0f * Time.deltaTime;
         }
     }

Any and all help is appreciated :)

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

2 Replies

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

Answer by unit_nick · Sep 11, 2017 at 01:04 AM

I think you need GetButton()

Returns true while the virtual button identified by buttonName is held down.

https://docs.unity3d.com/ScriptReference/Input.GetButton.html

GetButtonDown()

You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again.

https://docs.unity3d.com/ScriptReference/Input.GetButtonDown.html

so change line 12 to

 if (Input.GetButton ("Right")) {
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
1

Answer by Cuttlas-U · Sep 10, 2017 at 11:31 PM

hi; by buttons u mean buttons in the game ? or keys in the keyboard?

if number 1 is your problem then u cant do it like that u need to make it as a seperate function and call it when the button is pressed ( not put it in update);

send a screen shot so i can help u better;

Comment
Add comment · Show 5 · 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 prafull2001 · Sep 10, 2017 at 11:49 PM 0
Share

Yes, #1 is my problem.

Did you mean I should create a separate function like this:

  void movePlayer()
     {
         playerController.transform.position += Vector3.right * 15.0f * Time.deltaTime;
     }

So should I run this function on my button's "On Click()" property?

avatar image Cuttlas-U prafull2001 · Sep 11, 2017 at 12:42 AM 0
Share

yeah this is correct; but make it public so u can access it in button;

avatar image prafull2001 Cuttlas-U · Sep 11, 2017 at 12:36 PM 0
Share

Ok so I did that:

public void movePlayer(){

         playerController.transform.position += Vector3.right * 15.0f * Time.deltaTime;
         
     }

Then I added it to my "Left" button's OnClick() however it doesn't work when I hold the button pressed, only when I keep clicking it - how do I make it so "movePlayer" keeps running while button is pressed?

Sorry for asking so much, I am a noob :)

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

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

Related Questions

How to Detect if a Button is Not Being Pressed 0 Answers

How do I highlight multiple buttons at once? 1 Answer

Button Action Triggering Multiple Times Per Click? 1 Answer

How to make UI buttons so my player can move along certain lines 0 Answers

I need to move a sprite when an UI button is clicked 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