I have writing this Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D rb2D;
public bool grounded = true;
/*public Button moveLeftButton;
public Button moveRightButton;
public Button jumpButton;*/
private void OnTriggerStay2D(Collider2D collision)
{
grounded = true;
}
private void OnTriggerEnter2D(Collider2D collision)
{
grounded = true;
}
private void OnTriggerExit2D(Collider2D collision)
{
grounded = false;
}
void Start()
{
}
public List<RuntimeAnimatorController> skins;
void Update()
{
rb2D.velocity = new Vector2(0, rb2D.velocity.y);
if (Input.GetKey(KeyCode.RightArrow))
{
RightMove();
}
if (Input.GetKey(KeyCode.LeftArrow))
{
LeftMove();
}
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
if (transform.position.y < -8.85)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
public void RightMove()
{
rb2D.velocity = new Vector2(10, rb2D.velocity.y);
}
public void LeftMove()
{
rb2D.velocity = new Vector2(-10, rb2D.velocity.y);
}
public void Jump()
{
if (grounded)
{
rb2D.AddForce(new Vector2(0, 600));
}
}
/*public void ButtonMoves()
{
moveLeftButton.onClick.AddListener(LeftMove);
moveRightButton.onClick.AddListener(RightMove);
jumpButton.onClick.AddListener(Jump);
}*/
}
and access it to the button:
but it still doesnt works.
Can anyone Helps with that?