Unity:动画在动画器窗口中工作,但通过协程切换动画时角色在场景中冻结

问题描述 投票:0回答:1
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class CreatureController : MonoBehaviour
{
    public Animator animator; 
    public float speed = 3.0f;
    //public float turnSpeed = 200.0f; 
    public Vector3 boundaryMin; 
    public Vector3 boundaryMax; 

    private bool isWalking = false;
    private Vector3 targetDirection;

    private readonly string[] animationParameters = { "walk", "idle2", "sniff", "roar", "crouch" };

    void Start()
    {
        if (animator == null)
        {
            animator = GetComponent<Animator>();
        }
        targetDirection = transform.forward;

        SetRandomStartingPosition();
        StartCoroutine(PlayRandomAnimation());
    }

    void Update()
    {
        isWalking = animator.GetCurrentAnimatorStateInfo(0).IsName("Creep|Walk1_Action") || 
                    animator.GetCurrentAnimatorStateInfo(0).IsName("Creep|Crouch_Action");

        if (isWalking)
        {
            MoveCreature();
        }
    }
    
    void SetRandomStartingPosition()
    {
        float randomX = Random.Range(boundaryMin.x, boundaryMax.x);
        float randomZ = Random.Range(boundaryMin.z, boundaryMax.z);
        transform.position = new Vector3(randomX, transform.position.y, randomZ);
    }

    void MoveCreature()
    {
        transform.Translate(Vector3.forward * speed * Time.deltaTime);

        Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
        //transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * turnSpeed);

        Vector3 position = transform.position;
        if (position.x < boundaryMin.x || position.x > boundaryMax.x || position.z < boundaryMin.z || position.z > boundaryMax.z)
        {
            Vector3 directionToCenter = (new Vector3((boundaryMin.x + boundaryMax.x) / 2, position.y, (boundaryMin.z + boundaryMax.z) / 2) - position).normalized;
            targetDirection = directionToCenter;
        }
    }

    IEnumerator PlayRandomAnimation()
    {
        while (true)
        {
            int randomIndex = Random.Range(0, animationParameters.Length);
            string chosenAnimation = animationParameters[randomIndex];

            foreach (string parameter in animationParameters)
            {
                animator.SetBool(parameter, false);
            }

            animator.SetBool(chosenAnimation, true);
            Debug.Log($"Playing animation: {chosenAnimation}");

            float waitTime = Random.Range(2.0f, 5.0f);
            yield return new WaitForSeconds(waitTime);

            if (chosenAnimation == "Creep|Walk1_Action" || chosenAnimation == "Creep|Crouch_Action")
            {
                targetDirection = new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f)).normalized;
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("Triggered by: " + other.name);  
        if (other.CompareTag("Player"))
        {
            animator.SetBool("attack", true);
        }
    }
}

协程应该播放每个动画。我尝试成功,但场景中仅播放默认动画。当通过布尔参数切换到其他动画时,动画停止播放并且角色冻结。我希望协同程序启动并且动画将随机播放,直到玩家足够接近来攻击他们。

c# unity-game-engine game-development
1个回答
0
投票

您可能在正在使用的动画剪辑中记录了一个属性。如果您要通过脚本更改它,则需要将其删除。

© www.soinside.com 2019 - 2024. All rights reserved.