Unity3D 人工智能(三) 感知系统

Unity3D 人工智能(三) 感知系统

视觉可以分文两张 

第一种是判断距离  上一节已经讲过了

Unity3D 人工智能(三) 感知系统

这节课讲一下第二种  根据前方角度来判断

 

Unity3D 人工智能(三) 感知系统

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Soldier : MonoBehaviour {

    public float viewDistance = 5;
    public float viewAngle = 120; 
    public Transform PlayerTranform; 
    void Start () { 
        PlayerTranform = GameObject.Find("Player").transform; 
	} 
	void Update () { 
        if (Vector3.Distance(PlayerTranform.position, transform.position)<=viewDistance)
        {

            Vector3 playerDir = PlayerTranform.position - transform.position;

           float angle=    Vector3.Angle(playerDir, transform.forward);

            if (angle<= viewAngle/2)
            {
                Debug.Log("在视野范围内");
            }

        }

	}
}