简体   繁体   English

按特定视野检测播放器

[英]Detect player by specific field of view

I'm working on basic AI and would like my AI to detect player by specific field of view. 我正在研究基本的AI,并希望我的AI能够通过特定的视野来检测玩家。 The script it attached on the head of AI and AI does turn its heard with its animator. 它附在人工智能和人工智能头上的脚本确实让它的动画师听到了声音。

I was able to make AI detect player when facing the player directly, but it doesn't feel realistic? 当我直接面对玩家时,我能够让AI检测到玩家,但是感觉不真实吗?

public GameObject Player; 公共GameObject播放器; public bool isFacingPlayer; public bool isFacingPlayer;

void Update(){ void Update(){

    // check if AI is facing player
    Vector3 dir = Player.transform.position - transform.position;
    if (Vector3.Dot(dir, transform.forward) > 0.0f) {

        isFacingPlayer = true;

    } else {

        isFacingPlayer = false;

    }


}

I would like to have a specific field of view, say of 310. But not sure how to get that? 我想有一个特定的视野,比如310.但不知道如何得到它?

Uh, I don't really know why I asked for this question if I was able to figure it out on my own with a real logic. 呃,我真的不知道为什么我要问这个问题,如果我能够用一个真实的逻辑来解决这个问题。 Anyways, here's a fix if it'll help anyone: 无论如何,这是一个修复,如果它会帮助任何人:

Vector3 targetDir = Player.transform.position - transform.position;
        float angleToPlayer = (Vector3.Angle (targetDir, transform.forward));

        if (angleToPlayer >= -90 && angleToPlayer <= 90) { // 180 FOV
            isFacingPlayer = true;
        } else {
            isFacingPlayer = false;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM