add ckg
plantilla base para movimiento básico
This commit is contained in:
@@ -0,0 +1,347 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class WaypointCircuit : MonoBehaviour
|
||||
{
|
||||
public List<Transform> waypointList = new List<Transform> ();
|
||||
public bool showGizmo;
|
||||
public Color gizmoLabelColor;
|
||||
public bool useHandleForVertex;
|
||||
|
||||
public bool showVertexHandles;
|
||||
|
||||
public Color handleGizmoColor;
|
||||
public float gizmoRadius;
|
||||
public float handleRadius;
|
||||
|
||||
public bool smoothRoute = true;
|
||||
int numPoints;
|
||||
Vector3[] points;
|
||||
float[] distances;
|
||||
|
||||
[Range (1, 100)] public float editorVisualisationSubsteps = 100;
|
||||
|
||||
public float Length { get; set; }
|
||||
|
||||
//this being here will save GC allocs
|
||||
int p0n;
|
||||
int p1n;
|
||||
int p2n;
|
||||
int p3n;
|
||||
|
||||
float i;
|
||||
Vector3 P0;
|
||||
Vector3 P1;
|
||||
Vector3 P2;
|
||||
Vector3 P3;
|
||||
|
||||
// Use this for initialization
|
||||
void Awake ()
|
||||
{
|
||||
if (waypointList.Count > 1) {
|
||||
CachePositionsAndDistances ();
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 getClosestPosition (Vector3 position)
|
||||
{
|
||||
float minDistance = Mathf.Infinity;
|
||||
|
||||
int index = -1;
|
||||
|
||||
for (int i = 0; i < waypointList.Count; ++i) {
|
||||
float distance = GKC_Utils.distance (position, waypointList [i].position);
|
||||
|
||||
if (distance < minDistance) {
|
||||
minDistance = distance;
|
||||
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (index > -1) {
|
||||
return waypointList [index].position;
|
||||
} else {
|
||||
return Vector3.zero;
|
||||
}
|
||||
}
|
||||
|
||||
public float getProgressValue (Vector3 position)
|
||||
{
|
||||
float minDistance = Mathf.Infinity;
|
||||
|
||||
int index = -1;
|
||||
|
||||
for (int i = 0; i < waypointList.Count; ++i) {
|
||||
float distance = GKC_Utils.distance (position, waypointList [i].position);
|
||||
|
||||
if (distance < minDistance) {
|
||||
minDistance = distance;
|
||||
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (index > -1) {
|
||||
|
||||
Vector3 closestPosition = waypointList [index].position;
|
||||
|
||||
float totalDistance = 0;
|
||||
|
||||
if (index > 0) {
|
||||
Vector3 currentPosition = waypointList [0].position;
|
||||
|
||||
for (int i = 1; i < index; ++i) {
|
||||
float distance = GKC_Utils.distance (currentPosition, waypointList [i].position);
|
||||
|
||||
totalDistance += distance;
|
||||
|
||||
currentPosition = waypointList [i].position;
|
||||
}
|
||||
|
||||
return totalDistance;
|
||||
|
||||
} else {
|
||||
float distance = GKC_Utils.distance (position, waypointList [0].position);
|
||||
|
||||
return distance;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public RoutePoint GetRoutePoint (float dist)
|
||||
{
|
||||
// position and direction
|
||||
Vector3 p1 = GetRoutePosition (dist);
|
||||
Vector3 p2 = GetRoutePosition (dist + 0.1f);
|
||||
|
||||
Vector3 delta = p2 - p1;
|
||||
|
||||
return new RoutePoint (p1, delta.normalized);
|
||||
}
|
||||
|
||||
public Vector3 GetRoutePosition (float dist)
|
||||
{
|
||||
numPoints = waypointList.Count;
|
||||
|
||||
int point = 0;
|
||||
|
||||
if (Length == 0) {
|
||||
Length = distances [distances.Length - 1];
|
||||
}
|
||||
|
||||
dist = Mathf.Repeat (dist, Length);
|
||||
|
||||
while (distances [point] < dist) {
|
||||
++point;
|
||||
}
|
||||
|
||||
|
||||
// get nearest two points, ensuring points wrap-around start & end of circuit
|
||||
p1n = ((point - 1) + numPoints) % numPoints;
|
||||
p2n = point;
|
||||
|
||||
// found point numbers, now find interpolation value between the two middle points
|
||||
|
||||
i = Mathf.InverseLerp (distances [p1n], distances [p2n], dist);
|
||||
|
||||
if (smoothRoute) {
|
||||
// smooth catmull-rom calculation between the two relevant points
|
||||
|
||||
// get indices for the surrounding 2 points, because
|
||||
// four points are required by the catmull-rom function
|
||||
p0n = ((point - 2) + numPoints) % numPoints;
|
||||
p3n = (point + 1) % numPoints;
|
||||
|
||||
// 2nd point may have been the 'last' point - a dupe of the first,
|
||||
// (to give a value of max track distance instead of zero)
|
||||
// but now it must be wrapped back to zero if that was the case.
|
||||
p2n = p2n % numPoints;
|
||||
|
||||
P0 = points [p0n];
|
||||
P1 = points [p1n];
|
||||
P2 = points [p2n];
|
||||
P3 = points [p3n];
|
||||
|
||||
return CatmullRom (P0, P1, P2, P3, i);
|
||||
} else {
|
||||
// simple linear lerp between the two points:
|
||||
|
||||
p1n = ((point - 1) + numPoints) % numPoints;
|
||||
p2n = point;
|
||||
|
||||
return Vector3.Lerp (points [p1n], points [p2n], i);
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 CatmullRom (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float i)
|
||||
{
|
||||
// comments are no use here... it's the catmull-rom equation.
|
||||
// Un-magic this, lord vector!
|
||||
return 0.5f * (2 * p1 + (-p0 + p2) * i + (2 * p0 - 5 * p1 + 4 * p2 - p3) * (i * i) + (-p0 + 3 * p1 - 3 * p2 + p3) * (i * i * i));
|
||||
}
|
||||
|
||||
void CachePositionsAndDistances ()
|
||||
{
|
||||
// transfer the position of each point and distances between points to arrays for
|
||||
// speed of lookup at runtime
|
||||
points = new Vector3[waypointList.Count + 1];
|
||||
|
||||
distances = new float[waypointList.Count + 1];
|
||||
|
||||
float accumulateDistance = 0;
|
||||
|
||||
for (int i = 0; i < points.Length; ++i) {
|
||||
Transform t1 = waypointList [(i) % waypointList.Count];
|
||||
Transform t2 = waypointList [(i + 1) % waypointList.Count];
|
||||
|
||||
if (t1 != null && t2 != null) {
|
||||
Vector3 p1 = t1.position;
|
||||
Vector3 p2 = t2.position;
|
||||
|
||||
points [i] = waypointList [i % waypointList.Count].position;
|
||||
|
||||
distances [i] = accumulateDistance;
|
||||
|
||||
accumulateDistance += (p1 - p2).magnitude;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addNewWayPoint ()
|
||||
{
|
||||
Vector3 newPosition = transform.position;
|
||||
|
||||
if (waypointList.Count > 0) {
|
||||
newPosition = waypointList [waypointList.Count - 1].position + waypointList [waypointList.Count - 1].forward;
|
||||
}
|
||||
|
||||
GameObject newWayPoint = new GameObject ();
|
||||
|
||||
newWayPoint.transform.SetParent (transform);
|
||||
|
||||
newWayPoint.transform.position = newPosition;
|
||||
|
||||
newWayPoint.name = (waypointList.Count + 1).ToString ();
|
||||
|
||||
waypointList.Add (newWayPoint.transform);
|
||||
|
||||
//Undo.RegisterCreatedObjectUndo (newWayPoint, "add new waypoint circuit");
|
||||
updateComponent ();
|
||||
}
|
||||
|
||||
public void addNewWayPointAtIndex (int index)
|
||||
{
|
||||
addNewWayPoint ();
|
||||
|
||||
GameObject currentWaypoint = waypointList [waypointList.Count - 1].gameObject;
|
||||
|
||||
currentWaypoint.transform.position = waypointList [index].position + waypointList [index].right * 3;
|
||||
|
||||
waypointList.Insert ((index + 1), currentWaypoint.transform);
|
||||
|
||||
waypointList.RemoveAt (waypointList.Count - 1);
|
||||
|
||||
renameWaypoints ();
|
||||
}
|
||||
|
||||
public void renameWaypoints ()
|
||||
{
|
||||
for (int i = 0; i < waypointList.Count; ++i) {
|
||||
waypointList [i].name = (i + 1).ToString ();
|
||||
|
||||
waypointList [i].SetParent (null);
|
||||
|
||||
waypointList [i].SetParent (transform);
|
||||
}
|
||||
|
||||
updateComponent ();
|
||||
}
|
||||
|
||||
public void resetWaypointsPositions ()
|
||||
{
|
||||
Vector3 targetPosition = Vector3.zero;
|
||||
|
||||
for (int i = 0; i < waypointList.Count; ++i) {
|
||||
|
||||
waypointList [i].localPosition = targetPosition;
|
||||
|
||||
targetPosition += Vector3.forward * 2;
|
||||
}
|
||||
|
||||
updateComponent ();
|
||||
}
|
||||
|
||||
public void updateComponent ()
|
||||
{
|
||||
GKC_Utils.updateComponent (this);
|
||||
|
||||
GKC_Utils.updateDirtyScene ("Update Waypoint Circut System", gameObject);
|
||||
}
|
||||
|
||||
void OnDrawGizmos ()
|
||||
{
|
||||
if (!showGizmo) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (GKC_Utils.isCurrentSelectionActiveGameObject (gameObject)) {
|
||||
DrawGizmos ();
|
||||
}
|
||||
}
|
||||
|
||||
void OnDrawGizmosSelected ()
|
||||
{
|
||||
DrawGizmos ();
|
||||
}
|
||||
|
||||
void DrawGizmos ()
|
||||
{
|
||||
if (showGizmo) {
|
||||
if (waypointList.Count > 1) {
|
||||
numPoints = waypointList.Count;
|
||||
|
||||
CachePositionsAndDistances ();
|
||||
|
||||
Length = distances [distances.Length - 1];
|
||||
|
||||
Gizmos.color = gizmoLabelColor;
|
||||
Vector3 prev = waypointList [0].position;
|
||||
|
||||
if (smoothRoute) {
|
||||
if (editorVisualisationSubsteps <= 0) {
|
||||
editorVisualisationSubsteps = 1;
|
||||
}
|
||||
for (float dist = 0; dist < Length; dist += Length / editorVisualisationSubsteps) {
|
||||
Vector3 next = GetRoutePosition (dist + 1);
|
||||
Gizmos.DrawLine (prev, next);
|
||||
prev = next;
|
||||
}
|
||||
Gizmos.DrawLine (prev, waypointList [0].position);
|
||||
} else {
|
||||
for (int n = 0; n < waypointList.Count; ++n) {
|
||||
Vector3 next = waypointList [(n + 1) % waypointList.Count].position;
|
||||
Gizmos.DrawLine (prev, next);
|
||||
prev = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct RoutePoint
|
||||
{
|
||||
public Vector3 position;
|
||||
public Vector3 direction;
|
||||
|
||||
public RoutePoint (Vector3 position, Vector3 direction)
|
||||
{
|
||||
this.position = position;
|
||||
this.direction = direction;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4cff8e1d60f86284caba64e89c89cdef
|
||||
timeCreated: 1517514238
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 40995
|
||||
packageName: Game Kit Controller - Shooter Melee Adventure Creator 3D + 2.5D
|
||||
packageVersion: 3.77g
|
||||
assetPath: Assets/Game Kit Controller/Scripts/Vehicles/AI/WaypointCircuit.cs
|
||||
uploadId: 814740
|
||||
@@ -0,0 +1,268 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class WaypointProgressTracker : MonoBehaviour
|
||||
{
|
||||
[Header ("Main Settings")]
|
||||
[Space]
|
||||
|
||||
public bool waypointActive;
|
||||
|
||||
// This script can be used with any object that is supposed to follow a
|
||||
// route marked out by waypoints.
|
||||
|
||||
// This script manages the amount to look ahead along the route,
|
||||
// and keeps track of progress and laps.
|
||||
|
||||
[SerializeField] float lookAheadForTargetOffset = 5;
|
||||
// The offset ahead along the route that the we will aim for
|
||||
|
||||
[SerializeField] float lookAheadForTargetFactor = .1f;
|
||||
// A multiplier adding distance ahead along the route to aim for, based on current speed
|
||||
|
||||
[SerializeField] float lookAheadForSpeedOffset = 10;
|
||||
// The offset ahead only the route for speed adjustments (applied as the rotation of the waypoint target transform)
|
||||
|
||||
[SerializeField] float lookAheadForSpeedFactor = .2f;
|
||||
// A multiplier adding distance ahead along the route for speed adjustments
|
||||
|
||||
[SerializeField] ProgressStyle progressStyle = ProgressStyle.SmoothAlongRoute;
|
||||
// whether to update the position smoothly along the route (good for curved paths) or just when we reach each waypoint.
|
||||
|
||||
[SerializeField] float pointToPointThreshold = 4;
|
||||
// proximity to waypoint which must be reached to switch target to next waypoint : only used in PointToPoint mode.
|
||||
|
||||
public enum ProgressStyle
|
||||
{
|
||||
SmoothAlongRoute,
|
||||
PointToPoint,
|
||||
}
|
||||
|
||||
// these are public, readable by other objects - i.e. for an AI to know where to head!
|
||||
public WaypointCircuit.RoutePoint targetPoint { get; set; }
|
||||
|
||||
public WaypointCircuit.RoutePoint speedPoint { get; set; }
|
||||
|
||||
public WaypointCircuit.RoutePoint progressPoint { get; set; }
|
||||
|
||||
[Space]
|
||||
[Header ("Debug")]
|
||||
[Space]
|
||||
|
||||
public bool showGizmo;
|
||||
|
||||
public float progressDistance;
|
||||
// The progress round the route, used in smooth mode.
|
||||
public int progressNum;
|
||||
// the current waypoint number, used in point-to-point mode.
|
||||
|
||||
[Space]
|
||||
[Header ("Components")]
|
||||
[Space]
|
||||
|
||||
public vehicleAINavMesh vehicleAI;
|
||||
|
||||
public Transform target;
|
||||
[SerializeField] WaypointCircuit circuit;
|
||||
public Transform vehicleTransform;
|
||||
|
||||
// A reference to the waypoint-based route we should follow
|
||||
|
||||
Vector3 lastPosition;
|
||||
// Used to calculate current speed (since we may not have a rigidbody component)
|
||||
float speed;
|
||||
// current speed of this object (calculated from delta since last frame)
|
||||
|
||||
// setup script properties
|
||||
Transform currentWaypoint;
|
||||
|
||||
Vector3 currentVehiclePosition;
|
||||
|
||||
void Start ()
|
||||
{
|
||||
// we use a transform to represent the point to aim for, and the point which
|
||||
// is considered for upcoming changes-of-speed. This allows this component
|
||||
// to communicate this information to the AI without requiring further dependencies.
|
||||
|
||||
// You can manually create a transform and assign it to this component *and* the AI,
|
||||
// then this component will update it, and the AI can read it.
|
||||
|
||||
if (waypointActive) {
|
||||
setTrackActiveState (true);
|
||||
}
|
||||
}
|
||||
|
||||
public void setWaypointActiveState (bool state)
|
||||
{
|
||||
waypointActive = state;
|
||||
|
||||
if (waypointActive) {
|
||||
setTrackActiveState (true);
|
||||
} else {
|
||||
setTrackActiveState (false);
|
||||
}
|
||||
}
|
||||
|
||||
public void activateTrackState ()
|
||||
{
|
||||
if (!waypointActive) {
|
||||
setTrackActiveState (true);
|
||||
}
|
||||
}
|
||||
|
||||
public void stopTrackState ()
|
||||
{
|
||||
if (waypointActive) {
|
||||
setTrackActiveState (false);
|
||||
}
|
||||
}
|
||||
|
||||
void setTrackActiveState (bool state)
|
||||
{
|
||||
bool circuitLocated = circuit != null;
|
||||
|
||||
if (!circuitLocated) {
|
||||
circuit = FindObjectOfType<WaypointCircuit> ();
|
||||
|
||||
circuitLocated = circuit != null;
|
||||
}
|
||||
|
||||
if (!circuitLocated) {
|
||||
state = false;
|
||||
|
||||
waypointActive = false;
|
||||
}
|
||||
|
||||
if (state) {
|
||||
resetTrackerState ();
|
||||
|
||||
if (circuit == null) {
|
||||
circuit = FindObjectOfType<WaypointCircuit> ();
|
||||
}
|
||||
|
||||
if (vehicleTransform == null) {
|
||||
vehicleTransform = transform;
|
||||
}
|
||||
|
||||
if (vehicleAI.useNavmeshActive) {
|
||||
vehicleAI.follow (target);
|
||||
}
|
||||
|
||||
vehicleAI.setDrivingState (true);
|
||||
|
||||
vehicleAI.setUsingTrackActive (true);
|
||||
} else {
|
||||
vehicleAI.setDrivingState (false);
|
||||
|
||||
vehicleAI.setUsingTrackActive (false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// reset the object to sensible values
|
||||
public void resetTrackerState ()
|
||||
{
|
||||
bool circuitLocated = circuit != null;
|
||||
|
||||
if (!circuitLocated) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (progressStyle == ProgressStyle.PointToPoint) {
|
||||
target.position = circuit.waypointList [progressNum].position;
|
||||
target.rotation = circuit.waypointList [progressNum].rotation;
|
||||
} else {
|
||||
target.position = vehicleTransform.position;
|
||||
target.rotation = vehicleTransform.rotation;
|
||||
|
||||
// Vector3 currentPosition = circuit.getClosestPosition (vehicleTransform.position);
|
||||
// progressDistance = currentPosition.magnitude * 0.5f;
|
||||
|
||||
progressDistance = circuit.getProgressValue (vehicleTransform.position);
|
||||
}
|
||||
}
|
||||
|
||||
void Update ()
|
||||
{
|
||||
if (waypointActive) {
|
||||
currentVehiclePosition = vehicleTransform.position;
|
||||
|
||||
if (progressStyle == ProgressStyle.SmoothAlongRoute) {
|
||||
// determine the position we should currently be aiming for
|
||||
// (this is different to the current progress position, it is a a certain amount ahead along the route)
|
||||
// we use lerp as a simple way of smoothing out the speed over time.
|
||||
if (Time.deltaTime > 0) {
|
||||
speed = Mathf.Lerp (speed, (lastPosition - currentVehiclePosition).magnitude / Time.deltaTime, Time.deltaTime);
|
||||
}
|
||||
|
||||
target.position = circuit.GetRoutePoint (progressDistance + lookAheadForTargetOffset + lookAheadForTargetFactor * speed).position;
|
||||
target.rotation = Quaternion.LookRotation (circuit.GetRoutePoint (progressDistance + lookAheadForSpeedOffset + lookAheadForSpeedFactor * speed).direction);
|
||||
|
||||
// get our current progress along the route
|
||||
progressPoint = circuit.GetRoutePoint (progressDistance);
|
||||
|
||||
Vector3 progressDelta = progressPoint.position - currentVehiclePosition;
|
||||
|
||||
if (Vector3.Dot (progressDelta, progressPoint.direction) < 0) {
|
||||
progressDistance += progressDelta.magnitude * 0.5f;
|
||||
}
|
||||
|
||||
lastPosition = currentVehiclePosition;
|
||||
} else {
|
||||
// point to point mode. Just increase the waypoint if we're close enough:
|
||||
|
||||
Vector3 targetDelta = target.position - currentVehiclePosition;
|
||||
|
||||
if (targetDelta.magnitude < pointToPointThreshold) {
|
||||
progressNum = (progressNum + 1) % circuit.waypointList.Count;
|
||||
}
|
||||
|
||||
currentWaypoint = circuit.waypointList [progressNum];
|
||||
|
||||
target.position = currentWaypoint.position;
|
||||
target.rotation = currentWaypoint.rotation;
|
||||
|
||||
// get our current progress along the route
|
||||
progressPoint = circuit.GetRoutePoint (progressDistance);
|
||||
|
||||
Vector3 progressDelta = progressPoint.position - currentVehiclePosition;
|
||||
|
||||
if (Vector3.Dot (progressDelta, progressPoint.direction) < 0) {
|
||||
progressDistance += progressDelta.magnitude;
|
||||
}
|
||||
|
||||
lastPosition = currentVehiclePosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnDrawGizmos ()
|
||||
{
|
||||
if (!showGizmo) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (GKC_Utils.isCurrentSelectionActiveGameObject (gameObject)) {
|
||||
DrawGizmos ();
|
||||
}
|
||||
}
|
||||
|
||||
void OnDrawGizmosSelected ()
|
||||
{
|
||||
DrawGizmos ();
|
||||
}
|
||||
|
||||
//draw the pivot and the final positions of every door
|
||||
void DrawGizmos ()
|
||||
{
|
||||
if (showGizmo) {
|
||||
if (Application.isPlaying) {
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawLine (vehicleTransform.position, target.position);
|
||||
Gizmos.DrawWireSphere (circuit.GetRoutePosition (progressDistance), 1);
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawLine (target.position, target.position + target.forward);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 33f14a46fbc3b2c4580c014231143423
|
||||
timeCreated: 1517514238
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 40995
|
||||
packageName: Game Kit Controller - Shooter Melee Adventure Creator 3D + 2.5D
|
||||
packageVersion: 3.77g
|
||||
assetPath: Assets/Game Kit Controller/Scripts/Vehicles/AI/WaypointProgressTracker.cs
|
||||
uploadId: 814740
|
||||
@@ -0,0 +1,50 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class remoteVehicleNavmeshOverride : MonoBehaviour
|
||||
{
|
||||
[Header ("Main Settings")]
|
||||
[Space]
|
||||
|
||||
public bool targetIsFriendly;
|
||||
|
||||
public bool targetIsObject;
|
||||
|
||||
public bool setAutoBrakeOnRemoveTargetState;
|
||||
|
||||
public bool autoBrakeOnRemoveTarget;
|
||||
|
||||
[Space]
|
||||
[Header ("Components")]
|
||||
[Space]
|
||||
|
||||
public vehicleAINavMesh mainVehicleAINavMesh;
|
||||
|
||||
public Transform targetTranform;
|
||||
|
||||
|
||||
public void setVehicleNavMeshTargetPosition ()
|
||||
{
|
||||
if (targetTranform == null) {
|
||||
targetTranform = transform;
|
||||
}
|
||||
|
||||
mainVehicleAINavMesh.follow (targetTranform);
|
||||
|
||||
mainVehicleAINavMesh.setTargetType (targetIsFriendly, targetIsObject);
|
||||
|
||||
if (setAutoBrakeOnRemoveTargetState) {
|
||||
mainVehicleAINavMesh.setAutoBrakeOnRemoveTargetState (autoBrakeOnRemoveTarget);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeVehicleNavmeshTarget ()
|
||||
{
|
||||
if (setAutoBrakeOnRemoveTargetState) {
|
||||
mainVehicleAINavMesh.setAutoBrakeOnRemoveTargetState (autoBrakeOnRemoveTarget);
|
||||
}
|
||||
|
||||
mainVehicleAINavMesh.removeTarget ();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 159a7fb1c5a257746aef4070a6890cc0
|
||||
timeCreated: 1678958769
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 40995
|
||||
packageName: Game Kit Controller - Shooter Melee Adventure Creator 3D + 2.5D
|
||||
packageVersion: 3.77g
|
||||
assetPath: Assets/Game Kit Controller/Scripts/Vehicles/AI/remoteVehicleNavmeshOverride.cs
|
||||
uploadId: 814740
|
||||
@@ -0,0 +1,810 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class vehicleAINavMesh : AINavMesh
|
||||
{
|
||||
[Space]
|
||||
[Header ("Vehicle AI Navmesh Custom Settings")]
|
||||
[Space]
|
||||
|
||||
public bool vehicleAIActive = true;
|
||||
|
||||
public bool useNavmeshActive;
|
||||
|
||||
public bool autoBrakeOnRemoveTarget;
|
||||
|
||||
[Space]
|
||||
|
||||
public bool checkCurrentTargetStateEnabled;
|
||||
public float checkCurrentTargetStateRate = 0.3f;
|
||||
|
||||
[Space]
|
||||
|
||||
// This script provides input to the car controller in the same way that the user control script does.
|
||||
// As such, it is really 'driving' the car, with no special physics or animation tricks to make the car behave properly.
|
||||
|
||||
// "wandering" is used to give the cars a more human, less robotic feel. They can waver slightly
|
||||
// in speed and direction while driving towards their target.
|
||||
|
||||
[SerializeField][Range (0, 1)] float m_CautiousSpeedFactor = 0.05f;
|
||||
// percentage of max speed to use when being maximally cautious
|
||||
[SerializeField][Range (0, 180)] float m_CautiousMaxAngle = 50f;
|
||||
// angle of approaching corner to treat as warranting maximum caution
|
||||
[SerializeField] float m_CautiousMaxDistance = 100f;
|
||||
// distance at which distance-based cautiousness begins
|
||||
[SerializeField] float m_CautiousAngularVelocityFactor = 30f;
|
||||
// how cautious the AI should be when considering its own current angular velocity (i.e. easing off acceleration if spinning!)
|
||||
[SerializeField] float m_SteerSensitivity = 0.05f;
|
||||
// how sensitively the AI uses steering input to turn to the desired direction
|
||||
[SerializeField] float m_AccelSensitivity = 0.04f;
|
||||
// How sensitively the AI uses the accelerator to reach the current desired speed
|
||||
[SerializeField] float m_BrakeSensitivity = 1f;
|
||||
// How sensitively the AI uses the brake to reach the current desired speed
|
||||
[SerializeField] float m_LateralWanderDistance = 3f;
|
||||
// how far the car will wander laterally towards its target
|
||||
[SerializeField] float m_LateralWanderSpeed = 0.1f;
|
||||
// how fast the lateral wandering will fluctuate
|
||||
[SerializeField][Range (0, 1)] float m_AccelWanderAmount = 0.1f;
|
||||
// how much the cars acceleration will wander
|
||||
[SerializeField] float m_AccelWanderSpeed = 0.1f;
|
||||
// how fast the cars acceleration wandering will fluctuate
|
||||
[SerializeField] BrakeCondition m_BrakeCondition = BrakeCondition.TargetDistance;
|
||||
// what should the AI consider when accelerating/braking?
|
||||
|
||||
// whether the AI is currently actively driving or stopped.
|
||||
|
||||
// 'target' the target object to aim for.
|
||||
[SerializeField] bool m_StopWhenTargetReached = false;
|
||||
// should we stop driving when we reach the target?
|
||||
[SerializeField] float m_ReachTargetThreshold = 2;
|
||||
// proximity to target to consider we 'reached' it, and stop driving.
|
||||
|
||||
public float maxSpeed;
|
||||
|
||||
[Space]
|
||||
[Header ("Obstacle Detection Settings")]
|
||||
[Space]
|
||||
|
||||
public LayerMask obstacleInFrontLayermask;
|
||||
public float obstacleInFrontRaycastDistance = 2;
|
||||
public float obstacleInFrontRaycastOffset = 2;
|
||||
|
||||
[Space]
|
||||
|
||||
public bool stopIfCertainObstacleLayerDetected;
|
||||
public LayerMask layermaskToStopIfDetected;
|
||||
public float capsuleCastRadiusToStopIfDetected;
|
||||
public float capsuleCastDistanceToStopIfDetected;
|
||||
|
||||
[Space]
|
||||
[Header ("Reverse Direction Settings")]
|
||||
[Space]
|
||||
|
||||
public float reversingAccelereationDuration = 3;
|
||||
|
||||
public float minWaitToActivateReversing = 2;
|
||||
|
||||
[Space]
|
||||
[Header ("Debug")]
|
||||
[Space]
|
||||
|
||||
public bool showVehicleDebugPrint;
|
||||
|
||||
public Vector2 currentInputValues;
|
||||
|
||||
public bool driving;
|
||||
|
||||
public bool usingTrackActive;
|
||||
|
||||
public bool reversingActiveTemporaly;
|
||||
|
||||
public bool obstacleToStopDetected;
|
||||
|
||||
[Space]
|
||||
|
||||
public bool currentTargetIsCharacter;
|
||||
public bool currentTargetIsVehicle;
|
||||
public bool currentTargetIsDriving;
|
||||
|
||||
[Space]
|
||||
[Header ("Components")]
|
||||
[Space]
|
||||
|
||||
public inputActionManager mainInputActionManager;
|
||||
public Rigidbody m_Rigidbody;
|
||||
public Transform m_Target;
|
||||
public Transform vehicleTransform;
|
||||
public vehicleHUDManager mainVehicleHudManager;
|
||||
public AIPatrolSystem AIPatrolManager;
|
||||
|
||||
float m_RandomPerlin;
|
||||
// A random value for the car to base its wander on (so that AI cars don't all wander in the same pattern)
|
||||
float m_AvoidOtherCarTime;
|
||||
// time until which to avoid the car we recently collided with
|
||||
float m_AvoidOtherCarSlowdown;
|
||||
// how much to slow down due to colliding with another car, whilst avoiding
|
||||
float m_AvoidPathOffset;
|
||||
// direction (-1 or 1) in which to offset path to avoid other car, whilst avoiding
|
||||
|
||||
float currentSpeed;
|
||||
|
||||
public enum BrakeCondition
|
||||
{
|
||||
NeverBrake,
|
||||
// the car simply accelerates at full throttle all the time.
|
||||
TargetDirectionDifference,
|
||||
// the car will brake according to the upcoming change in direction of the target. Useful for route-based AI, slowing for corners.
|
||||
TargetDistance,
|
||||
// the car will brake as it approaches its target, regardless of the target's direction. Useful if you want the car to
|
||||
// head for a stationary target and come to rest when it arrives there.
|
||||
}
|
||||
|
||||
Vector3 currentVelocity;
|
||||
|
||||
Vector2 moveInput;
|
||||
|
||||
float cautiousnessRequired;
|
||||
float spinningAngle;
|
||||
|
||||
float lastTimeReversingActive;
|
||||
|
||||
float lastTimeReversingDisabled;
|
||||
|
||||
float lastSteerDirection = 0;
|
||||
|
||||
bool AIPatrolManagerLocated;
|
||||
|
||||
float lastTimeCheckCurrentTargetState;
|
||||
|
||||
|
||||
[Space]
|
||||
[Space]
|
||||
|
||||
[TextArea (3, 10)]
|
||||
public string explanation = "Enable the field Use Navmesh Active on this component if you want to use navmesh on this AI vehicle" +
|
||||
"instead of the race track system, which is just to follow a group of waypoints.\n\n" +
|
||||
"The navmesh option allows to set the movement based on the navigation path to reach a target, along with using the AI patrol on" +
|
||||
"vheicles as well.";
|
||||
|
||||
|
||||
void Awake ()
|
||||
{
|
||||
// give the random perlin a random value
|
||||
m_RandomPerlin = Random.value * 100;
|
||||
|
||||
if (m_Rigidbody == null) {
|
||||
m_Rigidbody = GetComponent<Rigidbody> ();
|
||||
}
|
||||
|
||||
if (vehicleTransform == null) {
|
||||
vehicleTransform = transform;
|
||||
}
|
||||
|
||||
AIPatrolManagerLocated = AIPatrolManager != null;
|
||||
}
|
||||
|
||||
public override void Start ()
|
||||
{
|
||||
base.Start ();
|
||||
|
||||
if (gameObject.activeSelf) {
|
||||
mainVehicleHudManager.setUsedByAIState (true);
|
||||
}
|
||||
}
|
||||
|
||||
void FixedUpdate ()
|
||||
{
|
||||
if (!vehicleAIActive) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (usingTrackActive) {
|
||||
updateVehicleTrack ();
|
||||
}
|
||||
}
|
||||
|
||||
void updateVehicleTrack ()
|
||||
{
|
||||
currentVelocity = m_Rigidbody.linearVelocity;
|
||||
|
||||
currentSpeed = currentVelocity.magnitude * 2.23693629f;
|
||||
|
||||
if (m_Target == null || !driving) {
|
||||
// Car should not be moving,
|
||||
// use handbrake to stop
|
||||
currentInputValues = Vector2.zero;
|
||||
|
||||
mainInputActionManager.overrideInputValues (currentInputValues, -1, 1, true);
|
||||
} else {
|
||||
Vector3 fwd = vehicleTransform.forward;
|
||||
|
||||
if (currentVelocity.magnitude > maxSpeed * 0.1f) {
|
||||
fwd = currentVelocity;
|
||||
}
|
||||
|
||||
float desiredSpeed = maxSpeed;
|
||||
|
||||
// now it's time to decide if we should be slowing down...
|
||||
switch (m_BrakeCondition) {
|
||||
|
||||
case BrakeCondition.TargetDirectionDifference:
|
||||
|
||||
// the car will brake according to the upcoming change in direction of the target. Useful for route-based AI, slowing for corners.
|
||||
|
||||
// check out the angle of our target compared to the current direction of the car
|
||||
float approachingCornerAngle = Vector3.Angle (m_Target.forward, fwd);
|
||||
|
||||
// also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
|
||||
spinningAngle = m_Rigidbody.angularVelocity.magnitude * m_CautiousAngularVelocityFactor;
|
||||
|
||||
// if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
|
||||
cautiousnessRequired = Mathf.InverseLerp (0, m_CautiousMaxAngle, Mathf.Max (spinningAngle, approachingCornerAngle));
|
||||
|
||||
desiredSpeed = Mathf.Lerp (maxSpeed, maxSpeed * m_CautiousSpeedFactor, cautiousnessRequired);
|
||||
|
||||
break;
|
||||
|
||||
case BrakeCondition.TargetDistance:
|
||||
|
||||
// the car will brake as it approaches its target, regardless of the target's direction. Useful if you want the car to
|
||||
// head for a stationary target and come to rest when it arrives there.
|
||||
|
||||
// check out the distance to target
|
||||
Vector3 delta = Vector3.zero;
|
||||
|
||||
if (useNavmeshActive) {
|
||||
delta = new Vector3 (AIMoveInput.moveInput.x, 0, AIMoveInput.moveInput.z);
|
||||
} else {
|
||||
delta = m_Target.position - vehicleTransform.position;
|
||||
}
|
||||
|
||||
float distanceCautiousFactor = Mathf.InverseLerp (m_CautiousMaxDistance, 0, delta.magnitude);
|
||||
|
||||
// also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
|
||||
spinningAngle = m_Rigidbody.angularVelocity.magnitude * m_CautiousAngularVelocityFactor;
|
||||
|
||||
// if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
|
||||
cautiousnessRequired = Mathf.Max (Mathf.InverseLerp (0, m_CautiousMaxAngle, spinningAngle), distanceCautiousFactor);
|
||||
|
||||
desiredSpeed = Mathf.Lerp (maxSpeed, maxSpeed * m_CautiousSpeedFactor, cautiousnessRequired);
|
||||
|
||||
break;
|
||||
|
||||
case BrakeCondition.NeverBrake:
|
||||
break;
|
||||
}
|
||||
|
||||
// Evasive action due to collision with other cars:
|
||||
|
||||
// our target position starts off as the 'real' target position
|
||||
Vector3 offsetTargetPos = m_Target.position;
|
||||
|
||||
// if are we currently taking evasive action to prevent being stuck against another car:
|
||||
if (Time.time < m_AvoidOtherCarTime) {
|
||||
// slow down if necessary (if we were behind the other car when collision occured)
|
||||
desiredSpeed *= m_AvoidOtherCarSlowdown;
|
||||
|
||||
// and veer towards the side of our path-to-target that is away from the other car
|
||||
offsetTargetPos += m_AvoidPathOffset * m_Target.right;
|
||||
} else {
|
||||
// no need for evasive action, we can just wander across the path-to-target in a random way,
|
||||
// which can help prevent AI from seeming too uniform and robotic in their driving
|
||||
offsetTargetPos += ((Mathf.PerlinNoise (Time.time * m_LateralWanderSpeed, m_RandomPerlin) * 2 - 1) * m_LateralWanderDistance) * m_Target.right;
|
||||
}
|
||||
|
||||
// use different sensitivity depending on whether accelerating or braking:
|
||||
float accelBrakeSensitivity = (desiredSpeed < currentSpeed) ? m_BrakeSensitivity : m_AccelSensitivity;
|
||||
|
||||
// decide the actual amount of accel/brake input to achieve desired speed.
|
||||
float accel = Mathf.Clamp ((desiredSpeed - currentSpeed) * accelBrakeSensitivity, -1, 1);
|
||||
|
||||
// add acceleration 'wander', which also prevents AI from seeming too uniform and robotic in their driving
|
||||
// i.e. increasing the accel wander amount can introduce jostling and bumps between AI cars in a race
|
||||
accel *= (1 - m_AccelWanderAmount) + (Mathf.PerlinNoise (Time.time * m_AccelWanderSpeed, m_RandomPerlin) * m_AccelWanderAmount);
|
||||
|
||||
// calculate the local-relative position of the target, to steer towards
|
||||
Vector3 localTarget = vehicleTransform.InverseTransformPoint (offsetTargetPos);
|
||||
|
||||
// work out the local angle towards the target
|
||||
float targetAngle = Mathf.Atan2 (localTarget.x, localTarget.z) * Mathf.Rad2Deg;
|
||||
|
||||
// get the amount of steering needed to aim the car towards the target
|
||||
float steer = Mathf.Clamp (targetAngle * m_SteerSensitivity, -1, 1) * Mathf.Sign (currentSpeed);
|
||||
|
||||
// feed input to the car controller.
|
||||
|
||||
currentInputValues = new Vector2 (steer, accel);
|
||||
|
||||
mainInputActionManager.overrideInputValues (currentInputValues, accel, 0, true);
|
||||
|
||||
// if appropriate, stop driving when we're close enough to the target.
|
||||
if (m_StopWhenTargetReached && localTarget.magnitude < m_ReachTargetThreshold) {
|
||||
setDrivingState (false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnCollisionStay (Collision col)
|
||||
{
|
||||
// detect collision against other cars, so that we can take evasive action
|
||||
if (col.rigidbody != null) {
|
||||
var otherAI = applyDamage.getVehicle (col.gameObject);
|
||||
|
||||
if (otherAI != null) {
|
||||
// we'll take evasive action for 1 second
|
||||
m_AvoidOtherCarTime = Time.time + 1;
|
||||
|
||||
// but who's in front?...
|
||||
if (Vector3.Angle (vehicleTransform.forward, otherAI.transform.position - vehicleTransform.position) < 90) {
|
||||
// the other ai is in front, so it is only good manners that we ought to brake...
|
||||
m_AvoidOtherCarSlowdown = 0.5f;
|
||||
} else {
|
||||
// we're in front! ain't slowing down for anybody...
|
||||
m_AvoidOtherCarSlowdown = 1;
|
||||
}
|
||||
|
||||
// both cars should take evasive action by driving along an offset from the path centre,
|
||||
// away from the other car
|
||||
var otherCarLocalDelta = vehicleTransform.InverseTransformPoint (otherAI.transform.position);
|
||||
|
||||
float otherCarAngle = Mathf.Atan2 (otherCarLocalDelta.x, otherCarLocalDelta.z);
|
||||
|
||||
m_AvoidPathOffset = m_LateralWanderDistance * -Mathf.Sign (otherCarAngle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void enableOrDisableAIObject (bool state)
|
||||
{
|
||||
if (state) {
|
||||
if (!gameObject.activeSelf) {
|
||||
gameObject.SetActive (state);
|
||||
|
||||
pauseAI (false);
|
||||
|
||||
setUsedByAIState (state);
|
||||
}
|
||||
} else {
|
||||
if (mainVehicleHudManager.currentDriverIsAI ()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (gameObject.activeSelf) {
|
||||
pauseAI (true);
|
||||
|
||||
gameObject.SetActive (state);
|
||||
|
||||
setUsedByAIState (state);
|
||||
|
||||
mainInputActionManager.overrideInputValues (Vector2.zero, -1, 0, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void enableOrDisableAIObjectWithoutActivatingDrivingState (bool state)
|
||||
{
|
||||
if (state) {
|
||||
if (!gameObject.activeSelf) {
|
||||
gameObject.SetActive (state);
|
||||
|
||||
pauseAI (false);
|
||||
|
||||
setUsedByAIStateWithoutActivatingDrvingState (state);
|
||||
}
|
||||
} else {
|
||||
if (mainVehicleHudManager.currentDriverIsAI ()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (gameObject.activeSelf) {
|
||||
pauseAI (true);
|
||||
|
||||
gameObject.SetActive (state);
|
||||
|
||||
setUsedByAIStateWithoutActivatingDrvingState (state);
|
||||
|
||||
mainInputActionManager.overrideInputValues (Vector2.zero, -1, 0, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setUsedByAIState (bool state)
|
||||
{
|
||||
mainVehicleHudManager.setUsedByAIState (state);
|
||||
}
|
||||
|
||||
public void setUsedByAIStateWithoutActivatingDrvingState (bool state)
|
||||
{
|
||||
mainVehicleHudManager.setUsedByAIStateWithoutActivatingDrvingState (state);
|
||||
}
|
||||
|
||||
public void enableOrDisablePatrolState (bool state)
|
||||
{
|
||||
if (AIPatrolManagerLocated) {
|
||||
if (state) {
|
||||
if (!AIPatrolManager.isPatrolPaused ()) {
|
||||
|
||||
setPatrolPauseState (false);
|
||||
|
||||
AIPatrolManager.setClosestWayPoint ();
|
||||
|
||||
AIPatrolManager.setReturningToPatrolState (true);
|
||||
|
||||
setTargetType (false, true);
|
||||
|
||||
setPatrolState (false);
|
||||
}
|
||||
} else {
|
||||
setPatrolPauseState (true);
|
||||
|
||||
setPatrolState (false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void setPatrolTarget (Transform newTarget)
|
||||
{
|
||||
if (patrollingPaused) {
|
||||
return;
|
||||
}
|
||||
|
||||
follow (newTarget);
|
||||
|
||||
setTargetType (false, true);
|
||||
}
|
||||
|
||||
public void SetTarget (Transform target)
|
||||
{
|
||||
m_Target = target;
|
||||
|
||||
setDrivingState (true);
|
||||
}
|
||||
|
||||
public void setDrivingState (bool state)
|
||||
{
|
||||
driving = state;
|
||||
|
||||
reversingActiveTemporaly = false;
|
||||
|
||||
lastSteerDirection = 0;
|
||||
|
||||
lastTimeReversingActive = 0;
|
||||
|
||||
lastTimeReversingDisabled = 0;
|
||||
}
|
||||
|
||||
public void setUsingTrackActive (bool state)
|
||||
{
|
||||
usingTrackActive = state;
|
||||
}
|
||||
|
||||
public void setVehicleAIActiveState (bool state)
|
||||
{
|
||||
vehicleAIActive = state;
|
||||
}
|
||||
|
||||
public override void updateAIControllerInputValues ()
|
||||
{
|
||||
if (!usingTrackActive) {
|
||||
if (isFollowingTarget ()) {
|
||||
Vector3 fwd = vehicleTransform.forward;
|
||||
|
||||
if (currentVelocity.magnitude > maxSpeed * 0.1f) {
|
||||
fwd = currentVelocity;
|
||||
}
|
||||
|
||||
float desiredSpeed = maxSpeed;
|
||||
|
||||
Vector3 delta = new Vector3 (AIMoveInput.moveInput.x, 0, AIMoveInput.moveInput.z);
|
||||
|
||||
float distanceCautiousFactor = Mathf.InverseLerp (m_CautiousMaxDistance, 0, delta.magnitude);
|
||||
|
||||
// also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
|
||||
spinningAngle = m_Rigidbody.angularVelocity.magnitude * m_CautiousAngularVelocityFactor;
|
||||
|
||||
// if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
|
||||
cautiousnessRequired = Mathf.Max (Mathf.InverseLerp (0, m_CautiousMaxAngle, spinningAngle), distanceCautiousFactor);
|
||||
|
||||
desiredSpeed = Mathf.Lerp (maxSpeed, maxSpeed * m_CautiousSpeedFactor, cautiousnessRequired);
|
||||
|
||||
|
||||
Vector3 lastPosition = vehicleTransform.position;
|
||||
|
||||
// if (canReachCurrentTarget) {
|
||||
int pathCornersCount = pathCorners.Count;
|
||||
|
||||
if (pathCornersCount > 0) {
|
||||
if (pathCornersCount > 1) {
|
||||
lastPosition = pathCorners [1];
|
||||
} else {
|
||||
lastPosition = pathCorners [0];
|
||||
}
|
||||
} else {
|
||||
if (currentTarget != null) {
|
||||
lastPosition = currentTarget.position;
|
||||
}
|
||||
}
|
||||
// } else {
|
||||
//
|
||||
// }
|
||||
|
||||
m_Target.position = lastPosition;
|
||||
m_Target.rotation = Quaternion.LookRotation (delta);
|
||||
|
||||
// Evasive action due to collision with other cars:
|
||||
|
||||
// our target position starts off as the 'real' target position
|
||||
Vector3 offsetTargetPos = m_Target.position;
|
||||
|
||||
// if are we currently taking evasive action to prevent being stuck against another car:
|
||||
if (Time.time < m_AvoidOtherCarTime) {
|
||||
// slow down if necessary (if we were behind the other car when collision occured)
|
||||
desiredSpeed *= m_AvoidOtherCarSlowdown;
|
||||
|
||||
// and veer towards the side of our path-to-target that is away from the other car
|
||||
offsetTargetPos += m_AvoidPathOffset * m_Target.right;
|
||||
} else {
|
||||
// no need for evasive action, we can just wander across the path-to-target in a random way,
|
||||
// which can help prevent AI from seeming too uniform and robotic in their driving
|
||||
offsetTargetPos += ((Mathf.PerlinNoise (Time.time * m_LateralWanderSpeed, m_RandomPerlin) * 2 - 1) * m_LateralWanderDistance) * m_Target.right;
|
||||
}
|
||||
|
||||
// use different sensitivity depending on whether accelerating or braking:
|
||||
float accelBrakeSensitivity = (desiredSpeed < currentSpeed) ? m_BrakeSensitivity : m_AccelSensitivity;
|
||||
|
||||
// decide the actual amount of accel/brake input to achieve desired speed.
|
||||
float accel = Mathf.Clamp ((desiredSpeed - currentSpeed) * accelBrakeSensitivity, -1, 1);
|
||||
|
||||
// add acceleration 'wander', which also prevents AI from seeming too uniform and robotic in their driving
|
||||
// i.e. increasing the accel wander amount can introduce jostling and bumps between AI cars in a race
|
||||
accel *= (1 - m_AccelWanderAmount) + (Mathf.PerlinNoise (Time.time * m_AccelWanderSpeed, m_RandomPerlin) * m_AccelWanderAmount);
|
||||
|
||||
// calculate the local-relative position of the target, to steer towards
|
||||
Vector3 localTarget = vehicleTransform.InverseTransformPoint (offsetTargetPos);
|
||||
|
||||
// work out the local angle towards the target
|
||||
float targetAngle = Mathf.Atan2 (localTarget.x, localTarget.z) * Mathf.Rad2Deg;
|
||||
|
||||
// get the amount of steering needed to aim the car towards the target
|
||||
float steer = Mathf.Clamp (targetAngle * m_SteerSensitivity, -1, 1) * Mathf.Sign (currentSpeed);
|
||||
|
||||
// feed input to the car controller.
|
||||
|
||||
float inputAngleWithVehicle = Vector3.Angle (getDesiredVelocity (), vehicleTransform.forward);
|
||||
|
||||
// print (inputAngleWithVehicle);
|
||||
|
||||
float inputAngleWithVehicleABS = Math.Abs (inputAngleWithVehicle);
|
||||
|
||||
float brakeValue = 0;
|
||||
|
||||
if (reversingActiveTemporaly) {
|
||||
accel *= -1;
|
||||
|
||||
if (lastSteerDirection != 0) {
|
||||
steer = -lastSteerDirection;
|
||||
}
|
||||
|
||||
bool disableReversingResult = false;
|
||||
|
||||
if (Time.time > lastTimeReversingActive + reversingAccelereationDuration) {
|
||||
disableReversingResult = true;
|
||||
|
||||
if (showVehicleDebugPrint) {
|
||||
print ("disabling reversing direction after wait time");
|
||||
}
|
||||
}
|
||||
|
||||
if (inputAngleWithVehicleABS < 45) {
|
||||
disableReversingResult = true;
|
||||
|
||||
if (showVehicleDebugPrint) {
|
||||
print ("disabling reversing direction from right angle");
|
||||
}
|
||||
}
|
||||
|
||||
// print (Math.Abs (inputAngleWithVehicleABS - 180) + " " + Math.Abs (steer) + " " +
|
||||
// (Math.Abs (steer) < 0.3f) + "" + (Math.Abs (inputAngleWithVehicleABS - 180) < 20));
|
||||
// Math.Abs (steer) < 0.3f ||
|
||||
|
||||
if (lastSteerDirection == 0 && Math.Abs (inputAngleWithVehicleABS - 180) < 10 && Time.time > lastTimeReversingActive + 0.5f) {
|
||||
disableReversingResult = true;
|
||||
|
||||
if (showVehicleDebugPrint) {
|
||||
print ("disabling reversing direction after movement in opposite direction");
|
||||
}
|
||||
}
|
||||
|
||||
if (disableReversingResult) {
|
||||
reversingActiveTemporaly = false;
|
||||
|
||||
lastTimeReversingDisabled = Time.time;
|
||||
|
||||
lastSteerDirection = 0;
|
||||
}
|
||||
} else {
|
||||
if (lastTimeReversingDisabled == 0 || Time.time > lastTimeReversingDisabled + minWaitToActivateReversing) {
|
||||
if (inputAngleWithVehicleABS > 70) {
|
||||
if (checkIfObstacleInFront ()) {
|
||||
reversingActiveTemporaly = true;
|
||||
|
||||
lastTimeReversingActive = Time.time;
|
||||
|
||||
if (Math.Abs (steer) > 0.1f) {
|
||||
lastSteerDirection = steer;
|
||||
}
|
||||
|
||||
if (showVehicleDebugPrint) {
|
||||
print ("reversing direction from obstacle and direction too off");
|
||||
}
|
||||
}
|
||||
} else if (inputAngleWithVehicleABS > 60 && Mathf.Abs (currentVelocity.magnitude) < 10) {
|
||||
if (checkIfObstacleInFront ()) {
|
||||
reversingActiveTemporaly = true;
|
||||
|
||||
lastTimeReversingActive = Time.time;
|
||||
|
||||
if (Math.Abs (steer) > 0.1f) {
|
||||
lastSteerDirection = steer;
|
||||
}
|
||||
|
||||
if (showVehicleDebugPrint) {
|
||||
print ("reversing direction from obstacle and direction too off");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (stopIfCertainObstacleLayerDetected) {
|
||||
obstacleToStopDetected = checkIfObstacleToStopInFront ();
|
||||
|
||||
if (obstacleToStopDetected) {
|
||||
steer = 0;
|
||||
accel = 0;
|
||||
|
||||
brakeValue = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currentInputValues = new Vector2 (steer, accel);
|
||||
|
||||
mainInputActionManager.overrideInputValues (currentInputValues, accel, brakeValue, true);
|
||||
|
||||
setOnGroundState (mainVehicleHudManager.isVehicleOnGround ());
|
||||
} else {
|
||||
currentInputValues = Vector2.zero;
|
||||
|
||||
mainInputActionManager.overrideInputValues (currentInputValues, -1, 1, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (checkCurrentTargetStateEnabled) {
|
||||
if (Time.time > lastTimeCheckCurrentTargetState + checkCurrentTargetStateRate) {
|
||||
checkCurrentTargetState ();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool checkIfObstacleInFront ()
|
||||
{
|
||||
if (Physics.Raycast (vehicleTransform.position + obstacleInFrontRaycastOffset * vehicleTransform.forward,
|
||||
vehicleTransform.forward, obstacleInFrontRaycastDistance, obstacleInFrontLayermask)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool checkIfObstacleToStopInFront ()
|
||||
{
|
||||
Vector3 currentObjectPosition = vehicleTransform.position + obstacleInFrontRaycastOffset * vehicleTransform.forward;
|
||||
|
||||
Vector3 targetDirection = vehicleTransform.forward;
|
||||
|
||||
Vector3 point1 = currentObjectPosition;
|
||||
Vector3 point2 = point1 + capsuleCastDistanceToStopIfDetected * targetDirection;
|
||||
|
||||
// point1 = currentObjectPosition;
|
||||
// point2 = currentRayTargetPosition.position + capsuleCastDistanceToStopIfDetected * targetDirection;
|
||||
|
||||
RaycastHit [] hits = Physics.CapsuleCastAll (point1, point2, capsuleCastRadiusToStopIfDetected, targetDirection, 0, layermaskToStopIfDetected);
|
||||
|
||||
bool surfaceFound = hits.Length > 0;
|
||||
|
||||
if (surfaceFound) {
|
||||
// if (Physics.CapsuleCast (point1, point2, capsuleCastRadiusToStopIfDetected, targetDirection, capsuleCastDistanceToStopIfDetected, layermaskToStopIfDetected)) {
|
||||
if (showGizmo) {
|
||||
Debug.DrawLine (point1, point2, Color.red, 2);
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
if (showGizmo) {
|
||||
Debug.DrawLine (point1, point2, Color.green, 2);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override void updateAICameraInputValues ()
|
||||
{
|
||||
if (!usingTrackActive) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public override void removeTarget ()
|
||||
{
|
||||
setTarget (null);
|
||||
|
||||
if (autoBrakeOnRemoveTarget) {
|
||||
mainVehicleHudManager.activateAutoBrakeOnGetOff ();
|
||||
}
|
||||
}
|
||||
|
||||
public override void checkStateOnSetTarget ()
|
||||
{
|
||||
checkCurrentTargetState ();
|
||||
}
|
||||
|
||||
void checkCurrentTargetState ()
|
||||
{
|
||||
if (checkCurrentTargetStateEnabled) {
|
||||
if (currentTarget != null) {
|
||||
currentTargetIsVehicle = applyDamage.isVehicle (currentTarget.gameObject);
|
||||
|
||||
currentTargetIsCharacter = applyDamage.isCharacter (currentTarget.gameObject) && !currentTargetIsVehicle;
|
||||
|
||||
if (currentTargetIsCharacter) {
|
||||
currentTargetIsDriving = applyDamage.isCharacterDriving (currentTarget.gameObject);
|
||||
|
||||
if (currentTargetIsDriving) {
|
||||
GameObject currentVehicle = applyDamage.getCharacterCurrentVehicle (currentTarget.gameObject);
|
||||
|
||||
if (currentVehicle != null) {
|
||||
currentTarget = currentVehicle.transform;
|
||||
|
||||
currentTargetIsVehicle = true;
|
||||
|
||||
currentTargetIsCharacter = false;
|
||||
|
||||
if (showDebugPrint) {
|
||||
print ("update target to vehicle " + currentTarget.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
GameObject lastDriverGameObject = applyDamage.getLastDriver (currentTarget.gameObject);
|
||||
|
||||
if (lastDriverGameObject != null) {
|
||||
currentTargetIsDriving = applyDamage.isCharacterDriving (lastDriverGameObject);
|
||||
|
||||
if (!currentTargetIsDriving) {
|
||||
currentTarget = lastDriverGameObject.transform;
|
||||
|
||||
currentTargetIsVehicle = false;
|
||||
|
||||
currentTargetIsCharacter = true;
|
||||
|
||||
if (showDebugPrint) {
|
||||
print ("update target to character " + currentTarget.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lastTimeCheckCurrentTargetState = Time.time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setAutoBrakeOnRemoveTargetState (bool state)
|
||||
{
|
||||
autoBrakeOnRemoveTarget = state;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fb47950917292484e9b2b0448d8ba0fe
|
||||
timeCreated: 1517512879
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 40995
|
||||
packageName: Game Kit Controller - Shooter Melee Adventure Creator 3D + 2.5D
|
||||
packageVersion: 3.77g
|
||||
assetPath: Assets/Game Kit Controller/Scripts/Vehicles/AI/vehicleAINavMesh.cs
|
||||
uploadId: 814740
|
||||
Reference in New Issue
Block a user