Реализовал прыжок с помощью Input.GetKeyDown(KeyCode.Spase)
. Все работало нормально. После этого я добавил UI-кнопку прыжка и прикрепил на нее ту же самую функцию прыжка, что использовал до этого. В итоге прыжок с помощью кнопки в 2-3 раза ниже, чем с помощью нажатия на пробел.
Ситуация усугубилась, когда я попробовал поменять GetKeyDown
на GetKeyUp
. Теперь персонаж периодически прыгал высоко, а в остальное время низко, как при нажатии кнопки.
Вот код, отвечающий за управление персонажем:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroController : MonoBehaviour
{
//Oсновные параметры
[SerializeField] private float jumpForce;
[SerializeField] private float moveSpeed;
[SerializeField] private float maxSpeed;
[SerializeField] private float rotationSpeed;
[SerializeField] private bool isGrounded;
[SerializeField] private float explosionForce;
private float maxExplosionForce;
private float maxExplosionJumpHeight = 6f;
//Ссылки на компоненты
private MobileController mController;
private Animator ch_animator;
private Rigidbody rb;
private Explosion explosion;
// Параметры геймплея
private Vector3 moveVector// направление передвижения
{
get
{
var horizontal = mController.Horizontal();
var vertical = mController.Vertical();
return new Vector3(horizontal, 0.0f, vertical);
}
}
// Start is called before the first frame update
private void Start()
{
explosion = GetComponent<Explosion>();
rb = GetComponent<Rigidbody>();
/*ch_animator = GetComponent<Animator>();*/
mController = GameObject.FindGameObjectsWithTag("Joystick")[0].GetComponent<MobileController>();
maxExplosionForce = explosion.explosionForce;
}
// Update is called once per frame
private void Update()
{
GetJumpHeight();
}
private void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
Move();
}
private void Move()
{
//перемещение персонажа
if (moveVector.magnitude > 0.1f)
{
Quaternion rotation = Quaternion.LookRotation(moveVector);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
}
if (rb.velocity.magnitude < maxSpeed)
{
rb.AddForce(moveVector * moveSpeed, ForceMode.Impulse);//метод передвижения
}
}
public void Jump()
{
if (isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
public void BUTTON_Jump()
{
if (isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
private void GetJumpHeight()
{
if (!isGrounded && Mathf.Abs(rb.velocity.y) <= 0.2f)
{
explosionForce = maxExplosionForce / maxExplosionJumpHeight * transform.position.y;
}
}
void OnCollisionEnter(Collision collision)
{
IsGroundedUpate(collision, true);
if (collision.gameObject.CompareTag("Ground"))
{
explosion.Explode(explosionForce);
explosionForce = 0f;
}
}
void OnCollisionExit(Collision collision)
{
IsGroundedUpate(collision, false);
}
private void OnCollisionStay(Collision collision)
{
IsGroundedUpate(collision, true);
}
private void IsGroundedUpate(Collision collision, bool value)
{
if (collision.gameObject.tag == ("Ground"))
{
isGrounded = value;
}
}
}
Как мне сделать так, чтобы персонаж всегда прыгал на одну высоту и при нажатии пробела, и при нажатии кнопки?