Hello, I’ve made a small game where I can move a player horizontally using a joystick and I would like to make him jump on a UI button click. The issue I have is that the player won’t move even tho the button works.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb;
public Joystick joystick;
public float speed = 15f;
public float jumpHeight = 50f;
float moveX = 0;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (joystick.Horizontal >= 0.2f)
{
moveX = speed;
rb.velocity = new Vector2 (moveX, 0);
}
else if (joystick.Horizontal <= -0.2f)
{
moveX = -speed;
rb.velocity = new Vector2 (moveX, 0);
}
else
{
moveX = 0;
rb.velocity = new Vector2 (moveX, 0);
}
}
public void Jump()
{
rb.velocity = new Vector2 (moveX, jumpHeight);
}
public void StopJump()
{
rb.velocity = new Vector2 (moveX, 0);
}
}
This is my code. Everything works fine except for the last two functions. I have them both on an event trigger on the button.
Any help would be appreciated :).
