You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity播放死亡动画时角色悬浮空中的问题求助

Unity跑酷游戏死亡动画角色悬浮问题求助

我是Unity新手,正在开发一款小型跑酷游戏。目前整体进展顺利,但死亡动画存在问题:当计时器归零时或角色生命值耗尽时,死亡动画能正常播放,但角色会悬浮在空中。

我曾怀疑是CharacterController的问题,尝试修改其尺寸:

ctrl.height= 0.16;
ctrl.radius= 0.14;

还尝试了射线检测来调整角色位置:

RaycastHit hit;

// 从玩家当前位置向下发射射线
if (Physics.Raycast(transform.position, Vector3.down, out hit, Mathf.Infinity))
{
    print("Raycast");
    // 根据与地面的碰撞点设置角色Y轴位置
    transform.position = new Vector3(transform.position.x, hit.point.y, transform.position.z);
}

遗憾的是这些方法都无效,且相关判断语句运行正常。我实在不知道该如何解决,以下是我的完整代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Movement : MonoBehaviour
{

    [SerializeField]private GameObject endtext;
    [SerializeField]private GameObject restart;
    [SerializeField]private GameObject dust;
    [SerializeField]private GameObject Timertext;
    [SerializeField]private GameObject[] health;
    private CharacterController ctrl;
    private float jump;
    private Animator playerAnimator;
    private float gravity;
    private int counter;
    private float timer;
    public bool win;
    public bool gameover;
    // Start is called before the first frame update
    void Start()
    {

        gravity= 15f;
        counter= 0;
        timer= 25;
        gameover= false;
        win= false;
        endtext.SetActive(false);
        restart.SetActive(false);
        ctrl = GetComponent<CharacterController>();
        playerAnimator = GetComponent<Animator>();
        playerAnimator.SetBool("IsRun", false);
        playerAnimator.SetBool("IsJump", false);
        playerAnimator.SetBool("IsDead", false);
        playerAnimator.SetBool("FirstState", true);
    }
    
    // Update is called once per frame
    void Update()
    {
        if (!(win) && !(gameover))
        {

        jump -=  gravity * Time.deltaTime;
        Vector3 direction= new Vector3(Input.GetAxis("Horizontal") * Time.deltaTime * 5, jump * Time.deltaTime, 0);
        ctrl.Move(direction);
        if(Input.GetAxis("Horizontal") > 0)
        {
            transform.rotation= Quaternion.Euler(0, 90, 0);
        }
        if(Input.GetAxis("Horizontal") < 0)
        {
            transform.rotation= Quaternion.Euler(0, 270, 0);
        }
        if(Input.GetKeyDown(KeyCode.Space) && (ctrl.isGrounded)) 
        
        {
            jump= 8;
            playerAnimator.SetBool("IsJump", true);
        }
        else
        {
            playerAnimator.SetBool("IsJump", false);
        }   

        if(direction.x != 0)
        {
            playerAnimator.SetBool("IsRun", true);
        }
        else
        {
            playerAnimator.SetBool("IsRun", false);
        }

        timer -= Time.deltaTime;
        Timertext.GetComponent<Text>().text= ((int)timer).ToString() + " Seconds";

        if(Vector3.Distance(transform.position,GameObject.Find("EndPoint").transform.position) <= 1)
        {
            win= true;
            
        }
        // print(Vector3.Distance(transform.position,GameObject.Find("EndPoint").transform.position));

        if(timer <= 0)
        {
            gameover= true;
        }
        }
        else
        {
            playerAnimator.SetBool("IsRun", false);
            playerAnimator.SetBool("IsJump", false);
                  
            if(gameover)
            {
                Die();
            }
            if(win)
            {
                endtext.GetComponent<Text>().text= "GOOD JOB!";
            }   
            EndScreen();
        }
    }

    void OnTriggerEnter(Collider other)
    {   
        if (!(win) && !(gameover))
        {
        if(other.tag=="Enemy")
        {    

            health[counter].GetComponent<Image>().enabled= false;
            counter ++;
            if (counter > health.Length -1)
            {
                counter= 0;
                gameover= true;
            }
            GameObject clones=Instantiate(dust, other.transform.position, Quaternion.identity);
            Destroy(clones, 1);
        }
        else if(other.tag=="Coin")
        {
            timer += 2;
            Destroy(other.gameObject);
        }
        else if(other.tag=="Chest")
        {
            timer += 5;
            Destroy(other.gameObject);
        }
        }

        
    }

    void Die()
    {

    // // 存储Raycast信息的变量
    RaycastHit hit;
    
    // 从玩家当前位置向下发射射线
    if (Physics.Raycast(transform.position, Vector3.down, out hit, Mathf.Infinity))
    {
        print("Raycast");
        // 根据与地面的碰撞点设置角色Y轴位置
        transform.position = new Vector3(transform.position.x, hit.point.y, transform.position.z);

    }

    playerAnimator.SetBool("FirstState", false);
    // 播放死亡动画
    playerAnimator.SetBool("IsDead",true);

    // 其他死亡相关代码
    endtext.GetComponent<Text>().text= "GAME OVER!";
    endtext.GetComponent<Text>().color= Color.red;
    }

    void EndScreen()
    {
        endtext.SetActive(true);
        restart.SetActive(true);
        // Timertext.transform.position= new Vector3(0, 0, 0);
    }

    public void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

}

内容的提问来源于stack exchange,提问作者Avisa Rasooli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 10:45:02