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 Power_Maker · Jan 26, 2018 at 10:41 PM · c#scripting problembuttonbuttons

Add force when button is pressed

I want to make my cube move right when u press button until you release it. When I press button called RightButton nothing happens.

     public Rigidbody rb;
 
     public float forwardForce = 2000f;
     public float sideForce = 200f;
 
     void FixedUpdate()
     {
         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
 
         if (Input.GetButton("RightButton"))
         {
             rb.AddForce(sideForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
         }
     }

Here's screen shot of this button:

alt text

I'm new user of unity so don't flame if i'm doing something wrong

lol.png (228.6 kB)
Comment
Add comment · Show 1
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 jman12EX · Jan 26, 2018 at 11:50 PM 0
Share

the "Button" you added that was not from the UI menu alt text alt text

then you say when pressed you increase the speed

 public void Addforce()
 {
     rb.AddForce(sideForce * Time.deltaTime, 0, 0, Force$$anonymous$$ode.VelocityChange);
 }

hope this helped

screenshot-34.png (469.1 kB)
rr.png (17.9 kB)

2 Replies

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

Answer by Power_Maker · Jan 28, 2018 at 08:50 PM

HEEL YEA I FOUND A SOLUTION. Here's the code:

     bool moveRight;
     public Rigidbody rb;
 
     public float forwardForce = 2000f;
     public float sideForce = 200f;
 
     // Update is called once per frame
     void FixedUpdate()
     {
         // Adding force to player
         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
 
 
         if (moveRight == true)
         {
             rb.AddForce(sideForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
         } else
         {
             rb.AddForce(0, 0, 0, 0);
         }
     }
 
     public void onPress()
     {
         
         moveRight = true;
     }
 
     public void onRelease()
     {
         
         moveRight = false;
     }

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 Power_Maker · Jan 27, 2018 at 09:43 AM

@jman12EX I did what u said, but there's one problem it adds force only once when I click, but I want it to add force every "Time.deltaTime".

 public Rigidbody rb;

 public float forwardForce = 2000f;
 public float sideForce = 200f;

 void FixedUpdate()
 {
     rb.AddForce(0, 0, forwardForce * Time.deltaTime);
 }

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

alt text

Now button was added from UI-Button alt text Am I doing something wrong again?

Or meaby I should do this as before but add this button to Edit-Project Settings-Input ?


szkic.png (66.0 kB)
szkic.png (11.6 kB)
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 jman12EX · Jan 27, 2018 at 11:16 PM 0
Share

so sorry i miss under stood this will work

    public Rigidbody rb;
 public float forwardForce = 2000f;
 public float sideForce = 200f;

 void FixedUpdate()
 {
     rb.AddForce(0, 0, forwardForce * Time.deltaTime);
 }
 public void onPress() //when press you move
 {
     rb.AddForce(sideForce * Time.deltaTime, 0, 0, Force$$anonymous$$ode.VelocityChange);
 }

 public void onRelease() //when not press you stop moving
 {
     rb.AddForce(0, 0, 0, 0);
 }

}

to do this you use an event trigger (sorry the image is not uploading i will try to explain)

on the button you add and new component by pressing "Add Component" and look for the "Event Trigger" then you press "Add New Event Type" you will need to add "Pointer Up" and Pointer Down" and pointer up is when it is pressed

avatar image Power_Maker jman12EX · Jan 28, 2018 at 08:23 PM 0
Share

It's same as before, adds force only once when I click. I think I need to put some commands to fixUpdate() or make some kind of loop. And I said it wrongly I want to move cube "every frame * Time.deltaTime"

avatar image Power_Maker Power_Maker · Jan 28, 2018 at 08:36 PM 0
Share

Or meaby there is some method to make button repeat it's function every frame?

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

136 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

Related Questions

Looking for an invisible button (that actually works!) 1 Answer

how can I disapear a button after push? 1 Answer

Scripting button functionality inside class with created object's method 1 Answer

Distribute terrain in zones 3 Answers

OnPointerDown and OnPointerUp doesn't work 6 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