Персонаж улетает вверх, а также кнопка прыжка не работает

Здраствуйте, такой вопрос. Второй день мучаюсь не могу сделать так чтобы персонаж прыгал при нажатии на кнопку на экране. На данный момент то чего я добился - это персонаж улетает вверх, а кнопка вовсе при нажатии ничего не делает. Помогите пожалуйста, как мне исправить код так чтобы все работало как надо.

введите сюда описание изображения

введите сюда описание изображения

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeroMove : MonoBehaviour
{
public int jumps;
private int jumpsCount;

public Transform groundCheck;
public LayerMask whatIsGround;
public float checkRadius;
private bool isGrounded;
Rigidbody2D rb;
private GameObject Button;

private JumpButton JumpButtonScript;

public float jumpForce;



// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
jumpsCount = jumps;
JumpButtonScript = GameObject.Find("Button").GetComponent<JumpButton>();



}

// Update is called once per frame
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius,whatIsGround);
}
void Update()
{

if(isGrounded == true)
{
jumpsCount = jumps;
}
Jump();

}
public void Jump()
{
jumpsCount--;
if(jumpsCount > 0){
rb.velocity = Vector2.up * jumpForce;
} else if(jumpsCount == 0 && isGrounded){
rb.velocity = Vector2.up * jumpForce;
}

}


}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeroMove : MonoBehaviour
{
public int jumps;
private int jumpsCount;

public Transform groundCheck;
public LayerMask whatIsGround;
public float checkRadius;
private bool isGrounded;
Rigidbody2D rb;
private GameObject Button;

private JumpButton JumpButtonScript;

public float jumpForce;



// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
jumpsCount = jumps;
JumpButtonScript = GameObject.Find("Button").GetComponent<JumpButton>();



}

// Update is called once per frame
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius,whatIsGround);
}
void Update()
{

if(isGrounded == true)
{
jumpsCount = jumps;
}
Jump();

}
public void Jump()
{
jumpsCount--;
if(jumpsCount > 0){
rb.velocity = Vector2.up * jumpForce;
} else if(jumpsCount == 0 && isGrounded){
rb.velocity = Vector2.up * jumpForce;
}

}


}using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeroMove : MonoBehaviour
{
public int jumps;
private int jumpsCount;

public Transform groundCheck;
public LayerMask whatIsGround;
public float checkRadius;
private bool isGrounded;
Rigidbody2D rb;
private GameObject Button;

private JumpButton JumpButtonScript;

public float jumpForce;



// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
jumpsCount = jumps;
JumpButtonScript = GameObject.Find("Button").GetComponent<JumpButton>();



}

// Update is called once per frame
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius,whatIsGround);
}
void Update()
{

if(isGrounded == true)
{
jumpsCount = jumps;
}
Jump();

}
public void Jump()
{
jumpsCount--;
if(jumpsCount > 0){
rb.velocity = Vector2.up * jumpForce;
} else if(jumpsCount == 0 && isGrounded){
rb.velocity = Vector2.up * jumpForce;
}

}


}

Код для кнопки :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;


public class JumpButton : MonoBehaviour, IPointerDownHandler
{
    public bool isPressed = false;
    public void OnPointerDown(PointerEventData eventData)
    {
        isPressed = true;
    }
    
}

Ответы (1 шт):

Автор решения: SLiver

сначала добавь кнопке скрипт ниже, а прошлый удали. У кнопки прыжка добавь компонент Event Trigger. Там нужно нажать Add New Event Type и выбрать PointerDown, потом перенести свою кнопку туда и выбрать JumpButton и там уже OnPointerDown. Потом в event trigger нужно нажать Add New Event Type и выбрать PointerUp, потом перенести свою кнопку туда и выбрать JumpButton и там уже OnPointerUp.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;


public class JumpButton : MonoBehaviour, IPointerDownHandler
{
    public bool isPressed = false;
    public void OnPointerDown()
    {
        isPressed = true;
    }
    public void OnPointerUp()
    {
        isPressed = false
    }
}

ошибка твоего скрипта кнопки прыжка в том, что при нажатии isPressed становилась true, и твой персонаж подлетал, а когда ты отпускал кнопку, то isPressed оставалась true из-за чего твой персонаж летел бесконечно вверх. может в другом скрипте нужно будет ещё уменьшить или прибавить jumpforce. вообще твой код похож на код из интернета, лучше подучи немного c# И unity, если ты их не знаешь. потом нужно полностью переделать другой скрипт. короче, вывод: ты не знаешь что делаешь и просто копируешь чужие скрипты.

→ Ссылка