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 /
avatar image
0
Question by martipello · Jan 24, 2016 at 04:08 PM · uibuttonjumpleftright

how to control player movement with ui buttons?

kinda explained it in the question, ive looked everywhere and found plenty of examples so i kinda know what im doing but not how to do it, my script right now manages keyboard keys for left right and jump through getkey "space" for jump but it uses getAxis for left and right, i want to add 3 buttons a left right and jump so what ive read i need to modify my existing scripts to contain a bool for left a bool for right and one for jump and in my button add an eventtrigger or onClick for the button and say moveRight = true or jump = true in pointer down and set it to false on pointer up but im just not sure where to add these bools or how to access them from my other script can anybody help please?

so first here is the important parts of my playerController

JUMP

  if (isGrounded)
     {
         if (Input.GetKeyDown("space"))
         {
             doMovement = true;
             isGrounded = false;
             rb.AddForce(0, jumpSpeed, 0);
           }
         else if (doMovement)
         {
             moveHorizontal = Input.GetAxis("Horizontal");
             Vector3 movement = new Vector3(moveHorizontal, 0, 0);
             rb.AddForce(movement * speed);
            
         }

WALKING

 if (isGrounded)
         {
             Vector3 movement = new Vector3(moveHorizontal, 0.0f, 0.0f); 
         }


So i think in my buttons script for jump i can just set isGrounded for jumping and doMovement for walking but it doesnt work for some reason, i also dont know how to check if im going left or right at the moment i just check the moveHorizontal variable and update my animation to show him going the other way, like this

  if (moveHorizontal > 0)
             {
                 animator.SetFloat("rollRight", 0);
                 animator.SetFloat("rollLeft", 2);
                 jumpingDirection = true;
              }
             if (moveHorizontal < 0)
             {
                 animator.SetFloat("rollLeft", 0);
                 animator.SetFloat("rollRight", 2);
                 jumpingDirection = false;
             }

but i dont think i can keep doing this from another script as it would lag should i just set up a bool in here like i have jumping direction and use that from my button script? im still really new to unity so please if im talking absolute gibberish please let me know many thanks

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
0
Best Answer

Answer by ShadyProductions · Jan 24, 2016 at 04:54 PM

Just make a Move method and whenever u press the button u call it with your params in it.
U can parse different things into it like, if it should move left or right, or jump etc.
Here's a small idea:

 void Move(string mDir) {
 if (mDir == "left") {
 //do moving left logic here
 } else if (mDir == "right") {
 //do moving right logic here
 } else if (mDir == "jump") {
 //do jumping logic here
 }
 }
Comment
Add comment · Show 3 · 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 martipello · Jan 24, 2016 at 05:16 PM 0
Share

im sorry i dont understand is this half an answer? are you saying this is to be saved in a separate script called move for example, and attached to each button and checked in the onClick of the button? if so how do i set myDir in my players movement script and give the value to the move script. for example my movement is

  Vector3 movement = new Vector3(moveHorizontal, 0.0f, 0.0f);

i cant put that in as my moveHorizontal float changes the direction which means id have to get a reference to the script, OR am i somehow adding that move method into my existing script? sorry for my ignorance but could you elaborate @ShadyProductions

avatar image ShadyProductions martipello · Jan 24, 2016 at 05:23 PM 0
Share

Its a method, that you can call it does things for you. $$anonymous$$aybe you should learn program$$anonymous$$g simpeler things before you try more advanced things?

If you are going to move using buttons U won't be needing to get the movedirection or anything like that? You're just gonna press Left, and the method $$anonymous$$ove("left") is called, which will know that it has to move 1 step left for example. And set the animations and other things etc.. It's all logical.

avatar image martipello ShadyProductions · Jan 24, 2016 at 05:49 PM 0
Share

i know what a method is, and after some trial and error i now realise that i can put this method in my player movement and pass it a string from the button. and ill be using both keyboard and buttons, so i will be needing it thanks but it still doesnt quite answer how i get a left and right state from getAxis i guess ill keep checking moveHorizontal,

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

UI jump button and isgrounded using Rigidbody 1 Answer

inheritance vs multiple component for UI 1 Answer

Problem with UI buttons and MultiTouch 4 Answers

Jumping on moving platforms problem c# 1 Answer

How to get component of any button that is clicked? (Using same function for every button) 2 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