【发布时间】:2022-01-15 05:42:04
【问题描述】:
Unity 动画存在问题。当角色跳跃时,会播放动画,但如果角色向任一方向跳跃,则动画不会停止,带有此动画的角色会在地面上滚动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovent : MonoBehaviour
{
public GameObject player;
public float speed = 10f;
private Rigidbody2D rb;
public int jump = 350 ;
Animator animation;
private bool inground;
// Start is called before the first frame update
void Start()
{
animation = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
var moveX = Input.GetAxis("Horizontal");
if (Input.GetKey(KeyCode.Space) && rb.velocity.y == 0)
{
rb.AddForce(Vector2.up * jump);
animation.SetTrigger("Jump");
}
rb.velocity = new Vector2(moveX * speed, rb.velocity.y);
if (animation)
{
animation.SetBool("Run", Mathf.Abs(moveX) >= 0.1f);
}
Vector3 charecterScale = transform.localScale;
if (Input.GetAxis("Horizontal") < 0)
{
charecterScale.x = -7.215315f;
}
if (Input.GetAxis("Horizontal") > 0)
{
charecterScale.x = 7.215315f;
}
transform.localScale = charecterScale;
}
}
【问题讨论】: