Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 播放器跳跃动画未在unity2d中播放_C#_Unity3d_2d - Fatal编程技术网

C# 播放器跳跃动画未在unity2d中播放

C# 播放器跳跃动画未在unity2d中播放,c#,unity3d,2d,C#,Unity3d,2d,我知道我的跳跃动画有一些问题已经有好几天了,我想知道如何使它这样当我按下“空格”键时,它会播放我的跳跃动画。我已经在我的动画师中有一个名为isJumping的bool函数,只是我不知道如何在scrpit中调用它来播放我的跳跃动画。我已经尝试了很多教程来找出我的答案,但大多数都没有很好地解释如何去做。我已经设置了行走动画,只是我知道需要知道如何进行跳跃动画 我的动画师的图像 到目前为止我的代码 using System.Collections; using System.Collections.

我知道我的跳跃动画有一些问题已经有好几天了,我想知道如何使它这样当我按下“空格”键时,它会播放我的跳跃动画。我已经在我的动画师中有一个名为isJumping的bool函数,只是我不知道如何在scrpit中调用它来播放我的跳跃动画。我已经尝试了很多教程来找出我的答案,但大多数都没有很好地解释如何去做。我已经设置了行走动画,只是我知道需要知道如何进行跳跃动画

我的动画师的图像

到目前为止我的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PM : MonoBehaviour
{


public float moveSpeed;
public float jumpHeight;
Rigidbody2D rb;
BoxCollider2D boxColliderPlayer;
int layerMaskGround;
float heightTestPlayer;
public Animator animator;

    void Start()
{
    rb = GetComponent<Rigidbody2D>();

    // Get the player's collider so we can calculate the height of the character.
    boxColliderPlayer = GetComponent<BoxCollider2D>();
    // We do the height test from the center of the player, so we should only check
    // halft the height of the player + some extra to ignore rounding off errors.
    heightTestPlayer = boxColliderPlayer.bounds.extents.y + 0.05f;
    // We are only interested to get colliders on the ground layer. If we would
    // like to jump ontop of enemies we should add their layer too (which then of
    // course can't be on the same layer as the player).
    layerMaskGround = LayerMask.GetMask("Ground");
}

    void Update()
    {
    float moveDir = Input.GetAxis("Horizontal") * moveSpeed;
    rb.velocity = new Vector2(moveDir, rb.velocity.y);

    // Your jump code:
    if (Input.GetKeyDown(KeyCode.Space) && IsGrounded() )
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
    }
    animator.SetFloat("Speed",Mathf.Abs(moveDir));
    }


    /// <summary>
    /// Simple check to see if our character is no the ground. 
    /// </summary>
    /// <returns>Returns <c>true</c> if the character is grounded.</returns>
    private bool IsGrounded()
    {
    // Note that we only check for colliders on the Ground layer (we don't want to hit ourself). 
    RaycastHit2D hit = Physics2D.Raycast(boxColliderPlayer.bounds.center, Vector2.down, heightTestPlayer, layerMaskGround);
    bool isGrounded = hit.collider != null;
    // It is soo easy to make misstakes so do a lot of Debug.DrawRay calls when working with colliders...
    Debug.DrawRay(boxColliderPlayer.bounds.center, Vector2.down * heightTestPlayer, isGrounded ? Color.green : Color.red, 0.5f);
    return isGrounded;
 }
} 
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共课PM:单一行为
{
公共交通速度;
公众浮标高度;
刚体2d rb;
BoxCollider2D BoxColliderLayer;
int layerMaskGround;
浮动高度测试播放器;
公共动画师;
void Start()
{
rb=GetComponent();
//获取玩家的碰撞器,以便我们可以计算角色的高度。
boxColliderPlayer=GetComponent();
//我们从球员的中心进行高度测试,所以我们只需要检查一下
//半身球员的身高+一些额外的忽略舍入错误。
heightTestPlayer=boxColliderPlayer.bounds.extents.y+0.05f;
//我们只对地面层的对撞机感兴趣
//喜欢跳到敌人的顶部,我们也应该添加他们的图层(然后是
//球场不能与球员在同一层)。
LayerMask地面=LayerMask.GetMask(“地面”);
}
无效更新()
{
float moveDir=Input.GetAxis(“水平”)*moveSpeed;
rb.velocity=新矢量2(moveDir,rb.velocity.y);
//您的跳转代码:
if(Input.GetKeyDown(KeyCode.Space)和&isground())
{
rb.velocity=新矢量2(rb.velocity.x,跳跃高度);
}
动画师.SetFloat(“速度”,Mathf.Abs(moveDir));
}
/// 
///简单检查一下我们的角色是否在地面上。
/// 
///如果角色固定,则返回true。
二等兵
{
//请注意,我们只检查地面层上的碰撞器(我们不想撞到自己)。
RaycastHit2D hit=Physics2D.Raycast(boxColliderPlayer.bounds.center,Vector2.down,heightTestPlayer,layerMaskGround);
bool isGrounded=hit.collider!=null;
//很容易出错,所以在使用碰撞器时要执行很多Debug.DrawRay调用。。。
Debug.DrawRay(boxColliderPlayer.bounds.center,Vector2.down*heightTestPlayer,isGrounded?Color.green:Color.red,0.5f);
返回是接地的;
}
} 

我不知道动画树是如何设置的。要使其工作,请从其他状态进行转换,如果isJump设置为true,则它将从任何状态转换为跳跃(当然跳跃除外),然后退出跳跃,返回到入口或空闲状态,或者在跳跃结束时的其他状态最有意义。如果你发布你的树的图像,我可以帮助你,如果需要的话

通过使用bool触发转换的转换和单独状态设置,播放动画所需的唯一代码应该是:

animator.SetBool("isJumping", true);
在IsGround函数中检测到播放机再次被固定后,使用相同的片段但传入false停止跳跃动画

animator.SetBool("isJumping", false);

如果我把我的动画师放进去,我会很高兴的;因为如果我把它放在私人牢房里,它就不会工作。你的牢房正在检查你是否牢房。把它放在那里总是会使它出错。仅当IsGround为false时设置isJumping。