SanAndreasUnity/Assets/Scripts/Behaviours/Ped/PedStalker.cs

90 lines
2.1 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using System.Collections.Generic;
using UnityEngine;
using SanAndreasUnity.Utilities;
using System.Linq;
namespace SanAndreasUnity.Behaviours
{
public class PedStalker : MonoBehaviour
{
public Ped MyPed { get; private set; }
public float stoppingDistance = 3;
public Ped TargetPed { get; set; }
2020-05-31 17:07:22 +00:00
void Awake ()
{
this.MyPed = this.GetComponentOrLogError<Ped> ();
}
void Update ()
{
// reset input
this.MyPed.ResetInput ();
// follow target ped
2020-05-31 17:07:22 +00:00
if (this.TargetPed != null) {
2020-05-31 17:07:22 +00:00
Vector3 targetPos = this.TargetPed.transform.position;
2020-05-31 17:07:22 +00:00
float currentStoppingDistance = this.stoppingDistance;
if (this.TargetPed.IsInVehicleSeat && !this.MyPed.IsInVehicle) {
2020-05-31 17:07:22 +00:00
// find a free vehicle seat to enter vehicle
var vehicle = this.TargetPed.CurrentVehicle;
2020-05-31 17:07:22 +00:00
var closestfreeSeat = Ped.GetFreeSeats (vehicle).Select (sa => new { sa = sa, tr = vehicle.GetSeatTransform (sa) })
.OrderBy (s => s.tr.Distance (this.transform.position))
.FirstOrDefault ();
if (closestfreeSeat != null) {
// check if it is in range
if (closestfreeSeat.tr.Distance (this.transform.position) < this.MyPed.EnterVehicleRadius) {
// the seat is in range
this.MyPed.EnterVehicle (vehicle, closestfreeSeat.sa);
} else {
// the seat is not in range
// move towards this seat
targetPos = closestfreeSeat.tr.position;
currentStoppingDistance = 0.1f;
}
}
} else if (!this.TargetPed.IsInVehicle && this.MyPed.IsInVehicleSeat) {
2020-05-31 17:07:22 +00:00
// target player is not in vehicle, and ours is
// exit the vehicle
this.MyPed.ExitVehicle ();
}
if (this.MyPed.IsInVehicle)
return;
Vector3 diff = targetPos - this.transform.position;
float distance = diff.magnitude;
if (distance > currentStoppingDistance)
{
Vector3 diffDir = diff.normalized;
this.MyPed.IsRunOn = true;
this.MyPed.Movement = diffDir;
this.MyPed.Heading = diffDir;
}
}
}
}
}