i tried so many ways… but settled finally with one*(the only one which worked)*
public Rigidbody rb;
public float ForwardForce = 2000f;
public float SideForce = 200f;
bool leftbtpressed, rightbtnpressed;
void Start()
{
rb = GetComponent<Rigidbody>();
leftbtpressed = false;
rightbtnpressed = false;
}
public void Leftbtndown()//add an eventtrigger to your left button and add "on pointer down() event"
{
leftbtpressed = true;
}
public void Leftbtnup()/add an eventtrigger to your left button and add "on pointer up() event"
{
leftbtpressed = false;
}
public void Rightbtndown()/add an eventtrigger to your right button and add "on pointer down() event"
{
rightbtnpressed = true;
}
public void Rightbtnup())/add an eventtrigger to your right button and add "on pointer up() event"
{
rightbtnpressed = false;
}
void FixedUpdate()
{
rb.AddForce(0, 0, ForwardForce * Time.deltaTime);
ToMoveorno();
}
public void ToMoveorno()
{
if (leftbtpressed)
{
rb.AddForce(-speed * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
else if (rightbtnpressed)
{
rb.AddForce(SideForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
after ataching this script to your player… add an event trigger component to both of your left and right btns… and then add 2 event types to both of your btns, one is: onpinterdown… and the other is: onponterup… and do the required…
if you dont know how to do this skip to 3:24 in ttis vid… but dont add what he adds in the vid to the event system instead add “onpinterdown” and “onponterup” events and do what you do for the default on click() that comes in a button.
also while finding that video i found another vid which uses the exact script like mine you can watch this instead of that...
I hope this helps helps you :)