【发布时间】:2021-06-24 16:53:48
【问题描述】:
我目前正在尝试学习如何统一制作 2d 游戏。我买了uedemy的课程。显然它不能很好地与移动部分配合使用。我创建了一个这样的动画循环:
显然在一些移动之后,他被卡住并且根本不制作任何动画(除了空闲动画)。
我不太懂这种语言,所以我不知道里面到底发生了什么。这是代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCtrl : MonoBehaviour
{
[Tooltip("this is a positive integer which speed up the player movement")]
public int speedBoost; // set this to 5
public float jumpSpeed; // set this to 600
Rigidbody2D rb;
SpriteRenderer sr;
Animator anim;
bool isJumping;
void Start()
{
rb = GetComponent<Rigidbody2D>();
sr = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
}
void Update()
{
float playerSpeed = Input.GetAxisRaw("Horizontal"); // value will be 1, -1 or 0
playerSpeed *= speedBoost;
if (playerSpeed != 0)
MoveHorizontal(playerSpeed);
else
StopMoving();
if (Input.GetButtonDown("Jump"))
Jump();
ShowFalling();
}
void MoveHorizontal(float playerSpeed)
{
rb.velocity = new Vector2(playerSpeed, rb.velocity.y);
if (playerSpeed < 0)
sr.flipX = true;
else if (playerSpeed > 0)
sr.flipX = false;
if (!isJumping)
anim.SetInteger("State", 1);
}
void StopMoving()
{
rb.velocity = new Vector2(0, rb.velocity.y);
if (!isJumping)
anim.SetInteger("State", 0);
}
void ShowFalling()
{
if (rb.velocity.y < 0)
{
anim.SetInteger("State", 3);
}
}
void Jump()
{
isJumping = true;
rb.AddForce(new Vector2(0, jumpSpeed)); // simply make the player jump in the y axis or upwards
anim.SetInteger("State", 2);
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Ground"))
isJumping = false;
}
}
如果有人能帮我解决这个问题,我很高兴。提前致谢。
【问题讨论】: