Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Sight: Sense {
- public int FieldOfView = 45;
- public int ViewDistance = 100;
- private Transform playerTrans;
- private Vector3 rayDirection;
- protected override void Initialize()
- {
- //Find player position
- playerTrans = GameObject.FindGameObjectWithTag("Player").transform;
- }
- // Update is called once per frame
- protected override void UpdateSense() {
- elapsedTime += Time.deltaTime;
- // Detect perspective sense if within the detection rate
- if (elapsedTime >= detectionRate) DetectAspect();
- }
- //Detect perspective field of view for the AI Character
- void DetectAspect() {
- RaycastHit hit;
- //Direction from current position to player position
- rayDirection = playerTrans.position -
- transform.position;
- //Check the angle between the AI character's forward
- //vector and the direction vector between player and AI
- if ((Vector3.Angle(rayDirection, transform.forward)) < FieldOfView) {
- // Detect if player is within the field of view
- if (Physics.Raycast(transform.position, rayDirection, out hit, ViewDistance))
- {
- if (hit.transform.tag == "Player")
- {
- print("Player detected");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement