首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否在到达目标位置后禁用脚本?

是否在到达目标位置后禁用脚本?
EN

Stack Overflow用户
提问于 2018-04-12 03:03:59
回答 1查看 41关注 0票数 0

当一个脚本到达目标玩家时,我如何让我的敌人禁用它?我想在我的代码中引用这个在我的GameManager下称为强制转换的脚本,并禁用它,但是我不确定当到达目标位置时如何编写函数。

代码语言:javascript
复制
public float lookRadius = 40f;

Transform target;
UnityEngine.AI.NavMeshAgent agent;

Rigidbody theRigidBody;

void Start()
{
    target = PlayerManager.instance.player.transform;
    agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}

void Update()
{
    float distance = Vector3.Distance (target.position, transform.position);
    if (distance <= lookRadius)
    {
        agent.SetDestination (target.position);

        if (distance <= agent.stoppingDistance)
        {
            FaceTarget ();
        }
    }
}

void FaceTarget()
{
    Vector3 direction = (target.position - transform.position).normalized;
    Quaternion lookRotation = Quaternion.LookRotation (new Vector3 (direction.x, 0, direction.z));
    transform.rotation = Quaternion.Slerp (transform.rotation, lookRotation, Time.deltaTime * 5f);
}


// Use this for initialization
public void OnObjectSpawn ()
{
    //myRender = GetComponent<Renderer> ();
    theRigidBody = GetComponent<Rigidbody>();
}

void OnDrawGizmosSelected()
{
    Gizmos.color = Color.red;
    Gizmos.DrawWireSphere (transform.position, lookRadius);
}
EN

回答 1

Stack Overflow用户

发布于 2018-04-12 03:07:20

您需要获取包含脚本的游戏对象,获取该游戏对象上的脚本,然后禁用该脚本。

代码语言:javascript
复制
if (distance < 1f) // or some distance
{
    Gameobject mygo = GameObject.Find("GameManager"); // or some other method to get gameobject.
    mygo.GetComponent<casting>().enabled = false;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49782686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档