Search Unity

[SOLVED] How to move RigidBody With Button?

Discussion in 'Scripting' started by HBK_, Jan 9, 2018.

  1. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    Hi everyone,i wanna know how can i add force to a rigidbody using UI Buttons and when i release them the force goes to zero,means the player stops..i found whole internet and found nothing which can do it....although i found a doc in untiy which shows adding force but didn't showed how to stop it after adding....Any Help?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  3. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    Like?....i'm totally confused cuz i've been scratching my head now from over 2 months and i'm not even able to get the controls working for mobile......so i wanna know what does OnPointerExit do? i mean does it work like on exiting any touch or just exiting form a specific touch like on exiting from click type touch or exiting from drag type touch?...hope you understand what i'm trying to say!.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,519
    https://unity3d.com/learn/tutorials...i/ui-events-and-event-triggers?playlist=17111

    Just make the button fire a method that does AddForce() on the rigidbody. Adding forces to a body is just pushing something. Friction and Drag (if there is any) will stop it once you quit pushing. You don't need to do any manual stopping. If you must, then you can just add opposing forces or modify the rigidbody velocity directly.

    Rather than spending months trying to figure out simple things on your own, try going through the Learn section of the site and doing a few tutorial series.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I know it's tough when you start and you are lost. You have to read, watch, practice, etc..
    I thought the down and exit pointer interfaces would allow you to have conditions that can tell when you're on the button and when it's released. IPointerExit is called when you leave the area of the raycast target (image/collider).

    I agree on the learn section. It's not as exciting, perhaps, as diving into your own game (whatever that may be), but it will certainly help to prepare you for aspects of the game you'd like to make.
    Perhaps some combination of the 2 is an acceptable compromise :)
     
  6. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    Okay Thanks Everyone I got it working and learned something new...This was the piece of code i was looking for Hope it'll help someone else.
    Code (CSharp):
    1. if (GUI.RepeatButton (new Rect (580, 580, 580, 590), "move")){
    2.  
    3.             rb.AddForce(Vector3.forward * 500);
    4.  
    5.      
    6.         }
    7.  
    After spending many hours on reading docs of Unity i finally found the button type which i was looking for and added forward force to rigidbody so it can move forward by adding
    Code (CSharp):
    1. Vector3.forward
    and did the same for left and right buttons and its working!

    When i press the OnGUI button the rigidbody moves forward and when i release it stops...thats what i was looking for...Thanks Once Again methos5k and LaneFox ....
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem. Glad you found what you were looking for.

    I think I remember saying this before, but in case I haven't. The OnGUI is not recommended for builds/general game dev.
     
  8. HBK_

    HBK_

    Joined:
    Dec 1, 2017
    Posts:
    87
    ya u said it before but this was working for me and doing exactly what i wanted so i guess its ok for me....but can i ask Why?.....
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, it's what most people "work with" in forum responses. You can make prefabs with them. They're easier to edit with images, masks, they're game objects instead of code (familiar for Unity).
    Anyways, it's working for you, that's cool :)
     
    HBK_ likes this.