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 AlexLundstrom · Mar 11, 2018 at 02:05 PM · rigidbodyaddforcemovingrigidbody.addforceevent triggering

How can I move with UI buttons without having to press many times?

So i'm trying to move player left and right by pressing UI buttons.

(This is the script) :

 void FixedUpdate()
         {
             rb.AddForce(0, 0, forwardforce * Time.deltaTime);
     
             if (Input.GetKey("d"))
             {
                 rb.AddForce(sidewaysforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
             }
             if (Input.GetKey("a"))
             {
                 rb.AddForce(-sidewaysforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
             }


 public void MoveLeft ()
 {
     rb.AddForce(-sidewaysforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
 }

 public void MoveRight()
 {
     rb.AddForce(sidewaysforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
 }

 public void SetVelocityZero()
 {
     rb.velocity = Vector3.zero;
 }`

and I have 2 event triggers in both of the UI buttons : pointer up and pointer down and MoveLeft and MoveRight and velocity thing are attached in event triggers. It works okay, but when I hold down button, the player moves only a little bit. I want, that if I hold the UI button down, it moves left/right as long until I release the UI button. How can I do that?? Any help is appreciated. :D

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
0

Answer by Chris_Payne_QS · Mar 11, 2018 at 02:19 PM

I don't think the Unity UI system has a repeating event for holding the UI button down. You may have to manually test each frame for the cursor hovering over the UI button AND the left mouse button being held down, so you can call MoveLeft/Right every frame.

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
0

Answer by Hellium · Mar 11, 2018 at 02:26 PM

I'm not an expert with the Physics in Unity, but try this:

 private Vector3 velocity ;
 
 void Update()
 {
          if (Input.GetKey( KeyCode.D ))
          {
              MoveLeft();
          }
          else if (Input.GetKey( KeyCode.A ))
          {
              MoveRight();
          }
          else if (Input.GetKeyUp( KeyCode.D ) || Input.GetKeyUp( KeyCode.A ))
          {
              SetVelocityZero();
          }
 }
 
 void FixedUpdate()
 {
      rb.AddForce( (velocity + Vector3.forward * forwardforce) * Time.fixedDeltaTime, ForceMode.VelocityChange );
 }
 
  public void MoveLeft ()
  {
      velocity = sidewaysforce;
  }
  public void MoveRight()
  {
      velocity = -sidewaysforce;
  }
  public void SetVelocityZero()
  {
      velocity = Vector3.zero;
  }


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 AlexLundstrom · Mar 13, 2018 at 02:05 PM 0
Share

@Hellium It didnt work. Its just the same. I can move only once if I press the UI button down.

avatar image Hellium AlexLundstrom · Mar 13, 2018 at 10:56 PM 1
Share

It works perfectly fine here.

I have :

  • a simple cube with the Rigidbody and the script with the provided code

  • Two buttons with EventTriggers with PointerDown and PointerUp
    • The first button calls $$anonymous$$oveLeft and SetVelocityZero

    • The second button calls $$anonymous$$oveRight and SetVelocityZero

When I hit run, if I click on the buttons, the rigidbody moves correctly

avatar image magotizila · Jul 18, 2019 at 08:37 PM 0
Share

Hellium if i have a button called "botaoDireita" how do i call him in my script? i mean... how do i activate a method when its clicked, how do i set it up in the "event trigger"?

avatar image Hellium magotizila · Jul 18, 2019 at 09:10 PM 1
Share

https://youtu.be/3NBYqPAA5Es?t=190

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

102 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

Related Questions

Physics in 2D mode not working? 0 Answers

Enemies are moving aroung wildly 1 Answer

add force to object that has 2 different rigid bodies 0 Answers

Force is being added to wrong RigidBody 1 Answer

How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 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