unity SetDestination无法移动

原因我也不知道。运行时,主角转圈,取消脚本,就可以了。

所以,我使用两个脚本,一个主脚本,一个判断主脚本活死的脚本。

主脚本代码:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

public class lsscript : MonoBehaviour

{

    public GameObject enemy;

    public float speed;

    

    private NavMeshAgent b;

    public GameObject object1;//chu xing shi

    public GameObject object2;//tong guan fang

    public GameObject object3;//empty object

    public int c=1;

    public bool c2;

    public AudioClip ls1;

    public AudioClip ls2;

    public bool jc;

    // Start is called before the first frame update

    // Update is called once per frame

    private void Start()

    {

        enemy = object1;

        

        

        b = GetComponent<NavMeshAgent>();

        b.SetDestination(enemy.transform.position);

        //var a = (enemy.transform.position - transform.position).normalized;

        //transform.Translate(a * speed * Time.deltaTime)

        GetComponent<ai>().enabled = true;

    }

    void Update()

    {

        //b.updateRotation = false;

        b.SetDestination(enemy.transform.position);

        

        //var a = (enemy.transform.position - transform.position).normalized;

        //transform.Translate(a * speed * Time.deltaTime)

     

        if (c == 2)

        {

            enemy=object2;

            

        }

        else if (c == 1)

        {

            enemy = object1;

        }else if (c == 3)

        {

            enemy = object3;

        }

        if (!c2)

        {

            if (!GetComponent<AudioSource>().isPlaying)

            {

                GetComponent<AudioSource>().loop = true;

                GetComponent<AudioSource>().clip = ls1;

                GetComponent<AudioSource>().Play();

            }

        }

        else if (c2)

        {

            if (!GetComponent<AudioSource>().isPlaying)

            {

                GetComponent<AudioSource>().loop = false;

                GetComponent<AudioSource>().clip = ls2;

                GetComponent<AudioSource>().Play();

            }

        }

    }

    private void OnTriggerEnter(Collider other)

    {

        if (other.name == "fhf") {

            object3 = other.gameObject;

            c = 3;

            c2 = true;

            b.speed = 6;

            StartCoroutine(six());

            jc = true;

            StartCoroutine(sixa());

        }//object1

        else if (other.name == "tgf")//tong guan fang

        {

            c = 1;

            jc = true;

            StartCoroutine(sixa());

        }

        else if (other.name == "cxs")

        {

            c = 2;

            jc = true;

            StartCoroutine(sixa());

        }

    }

    private void OnTriggerExit(Collider other)

    {

        if (other.name == "fhf")

        {

            c = 1;

            jc = false;

        }//object1

        else if (other.name == "tgf")//tong guan fang

        {

            c = 1;

            jc = false;

        }

        else if (other.name == "cxs")

        {

            c = 1;

            jc = false;

        }

    }

    IEnumerator six()

    {

        yield return new WaitForSeconds(16);

        c2 = false;

        speed = 3;

    }

    IEnumerator sixa()

    {

        yield return new WaitForSeconds(0.1f);

        jc = false;

    }

}

第二个脚本:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

public class ai : MonoBehaviour

{

  

    private void Update()

    {

        

        if (GetComponent<lsscript>().jc)

        {

            GetComponent<lsscript>().enabled = true;

        }

        else

        {

            GetComponent<lsscript>().enabled = false;

        }

    }

}

代码很简单,我就不多介绍了,这样就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值