I am developing a 2D video game for android, the only thing I need to finish is to make the touch controls. The video game only contains movement to the right and left and jump (like MarioBros). I have already created the buttons on canvas, I only need the script, I would greatly appreciate your help, I enclose my player code in case you can help me with the modifications.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public float jumpPower = 15f;
private bool jump;
public bool grounded;
private Rigidbody2D rb2d;
private Animator anim;
private GameObject healthbar;
public GameObject panelGameOver;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
healthbar = GameObject.Find("Healthbar");
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
jump = true;
}
anim.SetBool("Grounded", grounded);
}
void FixedUpdate()
{
{
if (Input.GetKey(KeyCode.RightArrow))
{
if (GetComponent<SpriteRenderer>().flipX == true)
{
GetComponent<SpriteRenderer>().flipX = false;
}
GetComponent<Animator>().SetBool("Correr", true);
transform.Translate(0.1f, 0, 0);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
if (GetComponent<SpriteRenderer>().flipX == false)
{
GetComponent<SpriteRenderer>().flipX = true;
}
GetComponent<Animator>().SetBool("Correr", true);
transform.Translate(-0.1f, 0, 0);
}
if (Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow))
{
GetComponent<Animator>().SetBool("Correr", false);
}
//Saltar
if (jump)
{
rb2d.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
jump = false;
}
}
//
}
public void EnemyJump()
{
jump = true;
}
public void EnemyJumpB()
{
healthbar.SendMessage("TakeDamage", 30);
jump = true;
}
}