• 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 bepalmer_unity · Feb 21, 2019 at 09:22 PM · movementbutton

Button click for movement

I have a UI button that triggers an event when it is clicked, and when it is released. The point of this is when I hit the "left" button, the player moves left. when it is released, the player stops. Same goes for the "right" button. I tried having a bool called righton and lefton, and then set these bools to true when the button is pressed, and false when released. This worked, and I could see in the console that the value was true when pressed, and false when released, which is what I wanted. However, if I tried sending the value in my FixedUpdate() the value was always false. Now I have the code below, that sets the SidewaysForce to 70, -70, or 0 based on if the buttons are pressed. I have a log statement that shows the value of SidewaysForce changing when I press or release the button. In the fixed update, I have it adding a force to my rigid body rb, but it is not working. any ideas?

 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.EventSystems;
 using UnityEngine.Events;
 using UnityEngine.UI;
 using System.Collections;
 public class PlayerMovement : MonoBehaviour
 {
     //rigid body of my player
     public Rigidbody rb;
     //a constant forward force
     public float ForForce=4845f;
     //the force of the sideways movement, manually set to 70 outside of code
     public float SideForce=400f;
     //variable used to determine the sideways force
     public float SidewaysForce;
 
 
     public void Start()
     {
         rb.useGravity = true;
     }
 
     //this function happens when I click the button or release the button. Pushing the button sets lval to 1, releasing it sets the lval to 2
     public void LeftButton(int lval)
     {
         if (lval == 1) //button is pressed
         {
             //calls the move left function, and shows me in the console if i pushed the button, which works
             moveleft();
             Debug.Log("left");
         }
         else           //button is released
         {
             //calls the dont move left function, and shows when the button is released, which is working
             Debug.Log("not left");
             dontmoveleft();
         }
     }
 
     //sets the sideways force 
     public void moveleft()
     {
         SidewaysForce = SideForce;
         Debug.Log(SidewaysForce); //shows the sideways force = 70 when it is pressed
     }
 
     //removes the sideways force by setting it = 0
     public void dontmoveleft()
     {
         SidewaysForce = 0f;
         Debug.Log(SidewaysForce); //shows me that the force is = 0 when released
     }
 
     //same as move left, but opposite direction
     public void moveright()
     {
         SidewaysForce = -SideForce;
         Debug.Log(SidewaysForce); //shows the force = -70 when pressed
     }
 
     //same as dontmoveleft
     public void dontmoveright()
     {
         SidewaysForce = 0f;
         Debug.Log(SidewaysForce);
     }
 
     //same as leftbutton
     public void RightButton(int rval)
     {
         reval = rval;
         if (rval == 1)
         {
             moveright();
             Debug.Log("right");
         }
         else if (rval==2)
         {
             Debug.Log("not right");
             dontmoveright();
         }
     }
     private void OnDestroy()
     {
         FindObjectOfType<GameManager>().EndGame();
     }
    
 
 
     // Update is called once per frame
     void FixedUpdate()
     { 
     rb.AddForce(0, 0, ForForce * Time.deltaTime);     //constant forward force
     rb.AddForce(SidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);   //should add the sideways force but doesnt work
 
 
 
         //ignore the rest of this
         if (Input.GetKey("m"))
         {
             SceneManager.LoadScene("menu");
         }
         if (transform.position.y<-1)
         {
             FindObjectOfType<Lives>().EndGame();
             FindObjectOfType<GameManager>().EndGame();
         }
         if(transform==null)
         {
             FindObjectOfType<Lives>().EndGame();
             FindObjectOfType<GameManager>().EndGame();
         }
 
     }
 }
 
Comment
Add comment · Show 11
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 xxmariofer · Feb 21, 2019 at 09:38 PM 0
Share

can you debug.log the sidewayforce in the fixedupdate and tell us if the value is correct? can you increase its value also for testing? change it to 7000 for testing rather than 70

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Move the player by holding a UI Button? 1 Answer

Move Player (y-Axis) on click button does not work 0 Answers

Variable value does not change 0 Answers

How can I add On-Screen Buttons to make the Ball move? 0 Answers

Move Character Left and Right UI buttons don't work 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