- Home /
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
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.
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;
}
@Hellium It didnt work. Its just the same. I can move only once if I press the UI button down.
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 PointerUpThe first button calls
$$anonymous$$oveLeft
andSetVelocityZero
The second button calls
$$anonymous$$oveRight
andSetVelocityZero
When I hit run, if I click on the buttons, the rigidbody moves correctly
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"?
Your answer
Follow this Question
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