2023-03-24 13:26:18 +08:00
|
|
|
|
using SHADE;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework
|
|
|
|
|
{
|
|
|
|
|
public class AILineOfSight:Script
|
|
|
|
|
{
|
|
|
|
|
public GameObject player;
|
|
|
|
|
|
|
|
|
|
public float range = 2.0f;
|
|
|
|
|
[Tooltip("Angle between player and forward to be within sight")]
|
|
|
|
|
public float angle = 30.0f;
|
|
|
|
|
|
|
|
|
|
[Tooltip("For debug")]
|
|
|
|
|
public float angleBetween = 0.0f;
|
|
|
|
|
|
|
|
|
|
[Tooltip("For debug")]
|
|
|
|
|
public float distance = 0.0f;
|
|
|
|
|
|
|
|
|
|
public float heightLimit = 1.0f;
|
|
|
|
|
|
|
|
|
|
public Vector3 rayOffset;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool withinRange;
|
|
|
|
|
public bool withinSight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Vector3 lastFoundPos;
|
|
|
|
|
public float lastFoundTimer = 0.0f;
|
|
|
|
|
|
|
|
|
|
protected override void update()
|
|
|
|
|
{
|
|
|
|
|
if (player == GameObject.Null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Transform transform = GetComponent<Transform>();
|
|
|
|
|
Transform playerTransform = player.GetComponent<Transform>();
|
|
|
|
|
Collider playerCollider = player.GetComponent<Collider>();
|
|
|
|
|
|
|
|
|
|
withinRange = false;
|
|
|
|
|
withinSight = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(transform && playerTransform && playerCollider)
|
|
|
|
|
{
|
|
|
|
|
Vector3 pos = transform.GlobalPosition + rayOffset;
|
|
|
|
|
Vector3 playerPos = playerTransform.GlobalPosition ;
|
|
|
|
|
Vector3 d = playerPos - pos;
|
|
|
|
|
distance = d.GetMagnitude();
|
|
|
|
|
if(distance < range)
|
|
|
|
|
{
|
|
|
|
|
Vector3 fwdHorizontal = transform.Forward;
|
|
|
|
|
fwdHorizontal.y = 0;
|
|
|
|
|
fwdHorizontal.Normalise();
|
|
|
|
|
|
|
|
|
|
Vector3 dHorizontal = d;
|
|
|
|
|
dHorizontal.y = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float dot = Vector3.Dot(fwdHorizontal, dHorizontal);
|
|
|
|
|
angleBetween = SHADE.Math.Rad2Deg * MathF.Acos(dot / (dHorizontal.GetMagnitude()));
|
|
|
|
|
if (angleBetween < angle && playerPos.y < pos.y + heightLimit)
|
|
|
|
|
{
|
|
|
|
|
withinRange = true;
|
|
|
|
|
withinSight = true;
|
|
|
|
|
Ray sightRay = new Ray(pos, d.GetNormalised());
|
|
|
|
|
List<RaycastHit> hitResults = Physics.Raycast(sightRay, distance,false, (ushort)65535);
|
|
|
|
|
|
|
|
|
|
foreach(RaycastHit hit in hitResults)
|
|
|
|
|
{
|
|
|
|
|
if (hit.Hit && hit.Other != player)
|
|
|
|
|
{
|
2023-03-25 14:51:28 +08:00
|
|
|
|
//Debug.Log("AI LOS: HIT OTHER");
|
2023-03-24 13:26:18 +08:00
|
|
|
|
withinSight = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (withinSight == true)
|
|
|
|
|
{
|
|
|
|
|
lastFoundPos = playerTransform.GlobalPosition;
|
|
|
|
|
lastFoundTimer = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lastFoundTimer += Time.DeltaTimeF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|