你好我正在尝试制作一个 2D 简单的平台游戏,我是 Unity 的初学者。出于某种原因,当我按下箭头键时,我的角色会播放向前的动画,但根本不会移动。然后当我释放它时播放空闲动画。它也不会跳。下面我附上了代码。我已经研究并尝试了许多修补刚体的方法,但都没有奏效。目前,我的播放器和地面都有一个多边形对撞机。我的角色也有一个刚体。我什至尝试添加调试日志来显示我的移动输入。到目前为止,当我按下箭头键时,移动输入会发生变化。所以,在我看来,我认为物理学有问题。
任何帮助将不胜感激,
谢谢
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
public float jumpforce;
private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform GroundCheck;
public float checkRadius;
public LayerMask whatIsground;
private int extraJumps;
public int extraJumpsValue;
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
moveInput = Input.GetAxis("Horizontal");
Debug.Log(moveInput);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if(facingRight == false && moveInput > 0)
{
Flip();
}else if(facingRight == true && moveInput < 0)
{
Flip();
}
}
void Update()
{
isGrounded = Physics2D.OverlapCircle(GroundCheck.position, checkRadius, whatIsground);
if (isGrounded == true)
{
extraJumps = extraJumpsValue;
}
if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpforce;
extraJumps--;
}else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpforce;
}
if (moveInput == 0)
{
anim.SetBool("isRunning", false);
}else
{
anim.SetBool("isRunning", true);
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler
}