c# - ac#统一跳转代码的一些问题
问题描述
这是我遇到问题的代码,事实上,当我按下空格键时,播放器只会重复跳跃动画,甚至右箭头也不再起作用。
请如果你能帮助你将非常受欢迎
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
Rigidbody2D rb2d;
public float Speed = 30f;
public float JumpForce = 30f;
bool Jump;
float speed;
private bool FacingRight;
// Start is called before the first frame update
void Start()
{
FacingRight = true;
rb2d = GetComponent<Rigidbody2D>();
rb2d.velocity = Vector2.zero;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
GetComponent<Animator>().SetBool("Jump", true);
}
if (Input.GetKey(KeyCode.RightArrow))
{
GetComponent<Animator>().SetFloat("speed", 1);
rb2d.velocity = Vector2.right * Speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
GetComponent<Animator>().SetFloat("speed", 1);
rb2d.velocity = Vector2.left * Speed * Time.deltaTime;
}
else
{
GetComponent<Animator>().SetFloat("speed", 0f);
rb2d.velocity = Vector2.zero;
}
}
private void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
Flip(horizontal);
}
private void Flip (float horizontal)
{
if (horizontal > 0 && !FacingRight || horizontal < 0 && FacingRight)
{
FacingRight = !FacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name ==" Front_Buildings")
{
GetComponent<Animator>().SetBool("isGrounded", true);
rb2d.velocity = Vector2.zero ;
}
}
}
解决方案
不要忘记将跳转设置回 false
GetComponent<Animator>().SetBool("Jump", false);
else
当左箭头被(不)按下时,你也有一个。所以你的右箭头(不是左箭头)会将速度和速度设置为 0。
推荐阅读
- c# - 使用无管理员帐户获取当前登录的用户 SID
- javascript - 将字符串格式化为 JSON 对象以在 HTML 中显示在带有 [(ngModel)] 的文本区域中
- npm - 防止 npm 每次都重建一切
- python - 有没有办法加快一些数据处理?
- azure - 将 JWT 传递给依赖方策略会导致“客户端断言无效”,原因是“jwt 格式”
- c# - 如何注册 Microsoft.ACE.OLEDB.12.0?
- python - 有没有办法将 3 值数组转换为图像?
- python-3.x - 如何使用 python 代码选择“many2many_checkboxes”的值
- git - 无法更新 docker 容器中的 git 模块
- angular - 我不能在 AppRoutingModule 中使用 TranslateService 吗?