当我按下“跳跃”键时,我的玩家并不总是跳起来.按钮
本文关键字:玩家 按钮 跳起来 我的 跳跃 键时 | 更新日期: 2023-09-27 18:13:18
在我的2D Unity项目中,我的玩家在按下"jump"按钮时并不总是跳跃。他在落地的那一刻不会跳,但在"落地"一秒钟后,他又可以跳了。这是什么问题呢?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class player : MonoBehaviour {
private static player instance;
public static player Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<player>();
}
return instance;
}
}
private Animator myAnimator;
[SerializeField]
public static float movementSpeed;
private bool facingRight = true;
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
[SerializeField]
private bool airControl;
[SerializeField]
private float jumpForce;
public bool canMove;
public AudioClip jump001;
public AudioClip jump002;
private float direction;
private bool move;
private float btnHorizontal;
public Rigidbody2D MyRigidbody { get; set; }
public bool Attack { get; set; }
public bool Jump { get; set; }
public bool OnGround { get; set; }
// Use this for initialization
void Start() {
facingRight = true;
MyRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}
void Update()
{
HandleInput();
}
// Update is called once per frame
void FixedUpdate()
{
OnGround = IsGrounded();
float horizontal = Input.GetAxis("Horizontal");
if (move)
{
this.btnHorizontal = Mathf.Lerp(btnHorizontal, direction, Time.deltaTime * 5);
HandleMovement(btnHorizontal);
Flip(direction);
}
else
{
HandleMovement(horizontal);
Flip(horizontal);
}
if (!canMove)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(0, GetComponent<Rigidbody2D>().velocity.y);
myAnimator.SetFloat("speed", 0);
return;
}
HandleLayers();
}
private void HandleMovement(float horizontal)
{
if (MyRigidbody.velocity.y < 0)
{
myAnimator.SetBool("land", true);
}
if (!Attack && (OnGround || airControl))
{
MyRigidbody.velocity = new Vector2(horizontal * movementSpeed, MyRigidbody.velocity.y);
}
if (Jump && MyRigidbody.velocity.y == 0)
{
SoundManager.instance.RandomizeSfx(jump001, jump002);
MyRigidbody.AddForce(new Vector2(0, jumpForce));
}
myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
}
private void HandleInput()
{
if (canMove)
{
//Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W) ||
if (Input.GetButtonDown("Jump"))
{
myAnimator.SetTrigger("jump");
}
if (Input.GetKeyDown(KeyCode.Z) || Input.GetButton("Fight") && OnGround && !Jump)
{
myAnimator.SetTrigger("attack");
}
}
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight && canMove)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private bool IsGrounded()
{
{
}
if (MyRigidbody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
return true;
}
}
}
}
return false;
}
private void HandleLayers()
{
if (!OnGround)
{
myAnimator.SetLayerWeight(1, 1);
}
else
{
myAnimator.SetLayerWeight(1, 0);
}
}
//TouchKnappar
public void BtnJump()
{
if (canMove)
{
myAnimator.SetTrigger("jump");
Jump = true;
}
}
public void BtnAttack()
{
myAnimator.SetTrigger("attack");
Attack = true;
}
public void BtnMove(float direction)
{
this.direction = direction;
this.move = true;
}
public void BtnStopMove()
{
this.direction = 0;
this.btnHorizontal = 0;
this.move = false;
}
public void BtnStopJump()
{
Jump = false;
}
}
我有同样的问题,这为我解决了它。
此代码标识字符是否基于FixedUpdate()
Collider2D groundCol = Physics2D.OverlapBox(groundCheck.position, groundBoxRadius, 0f, whatIsGround);
this.grounded = (groundCol != null && !groundCol.isTrigger && this.rigbody2d.velocity.y > -0.01f && this.rigbody2d.velocity.y < 0.01f);
一些解释:- groundCheck是字符 脚下的一个空对象。
- 在这种情况下,我正在检查叠加框是否与某个东西碰撞,如果那个东西不是触发器
- 最后,速度有时可能不完全是0,所以+-0.01f对我有效