浏览代码

-Added better AI Code. Still need to remove old

/dots-physics
Mike Geig 5 年前
当前提交
208b1c59
共有 5 个文件被更改,包括 147 次插入5 次删除
  1. 3
      Assets/Scripts/Boat/Engine.cs
  2. 8
      Assets/Unity Physics Items/Systems/InputSystem.cs
  3. 4
      Packages/manifest.json
  4. 126
      Assets/Unity Physics Items/AIController_DOTS2.cs
  5. 11
      Assets/Unity Physics Items/AIController_DOTS2.cs.meta

3
Assets/Scripts/Boat/Engine.cs


bool isHuman = GetComponent<BoatController>().Human;
if (!isHuman)
AIController_DOTS.Register(entity);
AIController_DOTS2.Register(entity, transform.position);
//AIController_DOTS.Register(entity);
var data = new DrivingData
{

8
Assets/Unity Physics Items/Systems/InputSystem.cs


protected override void OnUpdate()
{
//not time to start
//if (Time.time < startTime)
//return;
if (Time.time < startTime)
return;
Entities.ForEach((Entity entity, ref Translation pos, ref Rotation rot, ref DrivingData data ) =>
{

}
else
{
AIController_DOTS.GetInputs(entity, pos.Value, rot.Value, out data.throttle, out data.steering);
AIController_DOTS2.GetInputs(entity, pos.Value, rot.Value, out data.throttle, out data.steering);
if (data.throttle < .01f)
Debug.Log($"{Time.time} Throttle is zero");
}
});
}

4
Packages/manifest.json


{
"com.unity.properties" : "0.6.4-preview",
"com.unity.entities": "0.1.0-preview",
"com.unity.entities": "0.1.1-preview",
"com.unity.ide.rider": "1.1.0",
"com.unity.ide.vscode": "1.1.0",
"com.unity.inputsystem": "0.9.3-preview",

126
Assets/Unity Physics Items/AIController_DOTS2.cs


using BoatAttack;
using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.AI;
class PathData2
{
public Vector3[] pathPoints;
public int currentPathPoint;
public WaypointGroup.Waypoint currentWaypoint;
public bool foundPath;
public bool nearEnd;
public Vector3 storedPosition;
};
public class AIController_DOTS2 : MonoBehaviour
{
static AIController_DOTS2 main;
public float nearDistance = 8f;
Dictionary<Entity, PathData2> paths;
private void Awake()
{
if (main != null && main != this)
{
Destroy(this);
return;
}
main = this;
paths = new Dictionary<Entity, PathData2>();
}
private void Update()
{
foreach (var path in paths)
{
if (path.Value.nearEnd || !path.Value.foundPath)
CalculatePath(path.Value);
}
}
public static void Register(Entity entity, Vector3 pos)
{
if (main.paths.ContainsKey(entity))
return;
PathData2 data = new PathData2();
data.currentWaypoint = WaypointGroup.instance.GetClosestWaypoint(pos);
data.storedPosition = pos;
data.nearEnd = true;
main.paths.Add(entity, data);
}
public static void GetInputs(Entity entity, float3 pos, quaternion rot, out float throttle, out float steering)
{
main.GetInputsInternal(entity, pos, rot, out throttle, out steering);
}
void GetInputsInternal(Entity entity, float3 pos, quaternion rot, out float throttle, out float steering)
{
throttle = steering = 0;
//Do we have data?
PathData2 data;
if (!paths.TryGetValue(entity, out data) || data.pathPoints == null)
return;
data.storedPosition = pos; //Store position for navmesh calculations
if (data.currentPathPoint < data.pathPoints.Length && data.foundPath)
{
//Get angle to the destination and the side
Vector3 normDir = data.pathPoints[data.currentPathPoint] - (Vector3)pos;
normDir = normDir.normalized;
var forward = math.forward(rot);
float dot = Vector3.Dot(normDir, forward);
//float angle = Mathf.Acos (dot) * Mathf.Rad2Deg;
float targetSide = Vector3.Cross(forward, normDir).y;//positive on right side, negative on left side
steering = Mathf.Clamp(targetSide, -1.0f, 1.0f);
throttle = dot > 0 ? 1f : 0.25f;
if ((Vector3.Distance(pos, data.pathPoints[data.currentPathPoint])) < nearDistance) // If we are close to the current point on the path get the next
{
data.currentPathPoint++; // Move on to next point
if (data.currentPathPoint >= data.pathPoints.Length)
data.nearEnd = true;
}
}
}
void CalculatePath(PathData2 data)
{
var wp = WaypointGroup.instance.GetNextWaypoint(data.currentWaypoint);
var offset = (UnityEngine.Random.value * 2f - 1f) * wp.WPwidth * Vector3.left;
var curWPPos = wp.point + wp.rotation * offset;
var navPath = new NavMeshPath(); // New nav path
NavMesh.CalculatePath(data.storedPosition, curWPPos, 255, navPath);
if (navPath.status == NavMeshPathStatus.PathComplete) // if the path is good(complete) use it
{
data.currentWaypoint = wp;
data.pathPoints = navPath.corners;
data.currentPathPoint = 1;
data.foundPath = true;
data.nearEnd = false;
}
else if (navPath == null || navPath.status == NavMeshPathStatus.PathInvalid) // if the path is bad, we havent found a path
{
data.foundPath = false;
}
}
}

11
Assets/Unity Physics Items/AIController_DOTS2.cs.meta


fileFormatVersion: 2
guid: efe040b9229edbe42a6641620a4da643
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存