C# Unity 动画
Posted
技术标签:
【中文标题】C# Unity 动画【英文标题】:C# Unity Animation 【发布时间】:2022-01-15 05:42:04 【问题描述】:Unity 动画存在问题。当角色跳跃时,会播放动画,但如果角色向任一方向跳跃,则动画不会停止,带有此动画的角色会在地面上滚动。
https://ibb.co/hLKmK6W
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;
【问题讨论】:
【参考方案1】:解决方案是检查玩家是否接地,使用Physics2D.Raycast()
或Physics2D.OverlapCircle()
并将其作为布尔值传递给动画师,如下所示:
public float radius = .5f;
public LayerMask layermask;
Animator animation;
Rigidbody2D rb;
void Start()
animation = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
bool IsGrounded() => Physics2D.OverlapCircle(rb.position, radius, layermask);
// to visualize the circle
void OnDrawGizmosSelected()
Gizmos.color = Color.red;
if (rb != null) Gizmos.DrawWireSphere((Vector3)rb.position, radius);
void FixedUpdate()
animation.SetBool("isGrounded", IsGrounded());
【讨论】:
【参考方案2】:您需要检查您是否已接地。此视频中有一个示例:
https://youtu.be/CSu7MWv8qEY
【讨论】:
正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。以上是关于C# Unity 动画的主要内容,如果未能解决你的问题,请参考以下文章