首页 文章

预制件共享相同的动画

提问于
浏览
0

好吧,我有一个以编程方式克隆预制件的游戏对象(如产生敌人的塔防游戏),预制件上附有一个动画控制器 . 我的问题是无论从1个预制件播放什么动画,其他动画都共享相同的动画 . 例如,一个预制件在攻击动画中,其他预制件将执行相同的动画,我不希望这样 .

这是预制件的脚本:

using System;
using UnityEngine;
using Random = UnityEngine.Random;


public class AICharacterControl : MonoBehaviour
{
    public UnityEngine.AI.NavMeshAgent agent { get; private set; }                   
    [SerializeField] private GameObject[] GameObjects;                                    // target to aim for
    Transform target;
    Animator animator;
    int velocity = 0;
    Rigidbody rb;
    LayerMask layer;
    Collider collider;     
    private GameObject player;
    private Transform playerPosition;
    bool isMoving;

void Start()
    {

        animator = GetComponent<Animator>();
        rb = GetComponent<Rigidbody>();
        player = GameObject.FindGameObjectWithTag("Player");

        agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
        int n = Random.Range(0, GameObjects.Length);
        target = GameObjects[n].GetComponent<Transform>();

        agent.SetDestination(target.position);
        playerPosition = player.GetComponent<Transform>();



}


void Update()
    {

        if (Vector3.Distance(playerPosition.position, this.transform.position) < 10 && Vector3.Distance(playerPosition.position, this.transform.position) > 5 && player.transform.position.y < 6)
        {
        animator.SetBool("isMoving", true);
        agent.SetDestination(playerPosition.position);

            if (Vector3.Distance(playerPosition.position, this.transform.position) <= 5)
            {

                animator.SetBool("attack", true);

            }


        }

        else if (playerPosition.transform.position.y > 6)
        {

            agent.SetDestination(target.position);
            animator.SetBool("attack", false);

            if (agent.remainingDistance > agent.stoppingDistance)
            {

                animator.SetBool("isMoving", true);
                if (agent.remainingDistance <= 3)
                {

                    isMoving = false;


                }

            }


        }  

    }

这是产生预制件的游戏对象的脚本:

public class SpawnScript : MonoBehaviour {

// Use this for initialization
public GameObject NavAgent;
public int TotalSpawned = 0;
public int spawnDelay = 2;
public int SpawnQuantity;
public int SpawnRemaining;
public bool invoke = true;
private float time;
void Start () {

    SpawnRemaining = SpawnQuantity;

}

// Update is called once per frame
void Update() {

    time += Time.deltaTime;
    while (invoke == true && time >= spawnDelay)
    {
        GameObject spawner = Instantiate(NavAgent, this.transform.position, Quaternion.identity);     
        Invoke("SpawnAgent", spawnDelay);
        TotalSpawned++;
        time = 0;

        if (TotalSpawned == SpawnQuantity)
        {
            SpawnRemaining--;
            invoke = false;
        }

    }



}

1 回答

  • 0

    问题出在 if (playerPosition.transform.position.y > 6) . 它会检查所有预制件上的玩家's y position is greater than 6 and than attacks. Player'的位置是否相同,您需要比较玩家和预制件之间的距离 .

相关问题