我的主要问题是如何停止跳跃动画(触发后,它无限播放)。
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using System;
public class PlayerMovementScript : MonoBehaviour
{
[SerializeField] float runSpeed;
[SerializeField] float jumpSpeed;
Vector2 moveInput;
Rigidbody2D playerRigidbody;
Animator playerAnimator;
CapsuleCollider2D playerCapsuleCollider;
void Start()
{
playerRigidbody = GetComponent<Rigidbody2D>();
playerAnimator = GetComponent<Animator>();
playerCapsuleCollider = GetComponent<CapsuleCollider2D>();
}
void Update()
{
Run();
FlipSprite();
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
Debug.Log(moveInput);
}
void OnJump(InputValue value)
{
if(!playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }
if(value.isPressed)
{
playerAnimator.SetBool("isJumping", true);
playerRigidbody.velocity += new Vector2(0f, jumpSpeed);
}
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, playerRigidbody.velocity.y);
playerRigidbody.velocity = playerVelocity;
bool playerHasHorizontalSpeed = Math.Abs(playerRigidbody.velocity.x) > Mathf.Epsilon;
playerAnimator.SetBool("isRunning", playerHasHorizontalSpeed);
}
void FlipSprite()
{
bool playerHasHorizontalSpeed = Math.Abs(playerRigidbody.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed) {
transform.localScale = new Vector2(Mathf.Sign(playerRigidbody.velocity.x), 1f);
}
}
}
蚂蚁这是我现在的动画师:
布尔值:从空闲到跳转:
- isJumping = true从跳转到空闲:
- 正在跳转=假
- isRunning = false从跳转到运行:
- 正在跳转=假
- isRunning = true从空闲到运行:
- 正在跳转=假
- isRunning = true从运行到空闲:
- 正在运行=假
我尝试了一些尝试与isGrounded从这个教程,但改变后,我的角色只能跳跃和动画仍然工作不正确:https://www.youtube.com/watch?v=FTxQKHG5WCA
如何解决这个问题?
更新1:
我试过这段代码,但我的角色只能在更改后跳跃,动画仍然不起作用:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using System;
public class PlayerMovementScript : MonoBehaviour
{
[SerializeField] float runSpeed;
[SerializeField] float jumpSpeed;
[SerializeField] Transform groundCheckCollider;
[SerializeField] LayerMask groundLayer;
[SerializeField] bool isGrounded;
const float groundCheckRadius = 0.2f;
Vector2 moveInput;
Rigidbody2D playerRigidbody;
Animator playerAnimator;
CapsuleCollider2D playerCapsuleCollider;
void Start()
{
playerRigidbody = GetComponent<Rigidbody2D>();
playerAnimator = GetComponent<Animator>();
playerCapsuleCollider = GetComponent<CapsuleCollider2D>();
}
void Update()
{
Run();
FlipSprite();
GroundCheck();
playerAnimator.SetFloat("yVelocity", playerRigidbody.velocity.y);
}
void OnJump(InputValue value)
{
if(!playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }
if(value.isPressed)
{
playerAnimator.SetBool("isJumping", true);
playerRigidbody.velocity += new Vector2(0f, jumpSpeed);
}
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, playerRigidbody.velocity.y);
playerRigidbody.velocity = playerVelocity;
bool playerHasHorizontalSpeed = Math.Abs(playerRigidbody.velocity.x) > Mathf.Epsilon;
playerAnimator.SetBool("isRunning", playerHasHorizontalSpeed);
}
void FlipSprite()
{
bool playerHasHorizontalSpeed = Math.Abs(playerRigidbody.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed) {
transform.localScale = new Vector2(Mathf.Sign(playerRigidbody.velocity.x), 1f);
}
}
void GroundCheck()
{
isGrounded = false;
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckCollider.position, groundCheckRadius, groundLayer);
if (colliders.Length > 0)
{
isGrounded = true;
}
playerAnimator.SetBool("isJumping", !isGrounded);
}
}
1条答案
按热度按时间zwghvu4y1#
我是这样处理的:
动画师: