Hi, I’m working on a 2D platform Game for Android. As it is for mobile touch controls are required, So I have created UI Buttons for moving left & right.
My player movement is using AddForce in FixedUpdate, and my player is a PREFAB that gets instantiated after starting the level/Game. When I try to move the player using the button, its not working. I had used Event Trigger Pointer Up and down Types in the button to call the movement function bools. (It was working when I tried to move it using keyboard controls earlier so no problem with the movement code)
So I tried to move another object in the scene using same script and button, It worked and it wasn’t a prefab. So I believe it is an issue because it was a prefab.
Also i did tried to debug.log something under the movement code , but its not being called.
I have attached my code below for reference, anybody with a solution pls help.
void FixedUpdate()
{
if (moveLeft&& isOnGround )
{
playerRb.AddForce(Vector2.left * movementSpeed*Time.deltaTime, ForceMode2D.Force);
Debug.Log("moving Left");
}
if (moveRight && isOnGround)
{
playerRb.AddForce(Vector2.right * movementSpeed*Time.deltaTime, ForceMode2D.Force);
}
if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
{
playerRb.AddForce(Vector2.up*jumpSpeed, ForceMode2D.Impulse);
isOnGround = false;
}
}
public void LeftButton()
{
moveLeft = true;
moveRight = false;
}
public void RightButton()
{
moveRight = true;
moveLeft = false;
}
public void UpButton()
{
moveRight = false;
moveLeft = false;
}