Skip to content

Commit

Permalink
Improve AIPlayer script.
Browse files Browse the repository at this point in the history
  • Loading branch information
adrenak committed Jun 19, 2019
1 parent f2da369 commit 6d34f45
Show file tree
Hide file tree
Showing 159 changed files with 97 additions and 66 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2291,7 +2291,7 @@ GameObject:
- component: {fileID: 1424718898}
- component: {fileID: 1424718896}
m_Layer: 0
m_Name: Sphere
m_Name: Target
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
Expand Down Expand Up @@ -2343,7 +2343,7 @@ Transform:
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1424718895}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 26.75, y: 0, z: 45.09}
m_LocalPosition: {x: 17.84, y: 0, z: 50.68}
m_LocalScale: {x: 2.254064, y: 2.2540638, z: 2.2540638}
m_Children: []
m_Father: {fileID: 0}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public class AIDemo : MonoBehaviour {
public Transform destination;

void Start() {
var player = vehicle.gameObject.AddComponent<AIPlayer>();
vehicle.SetPlayer(player);
var ai = vehicle.gameObject.AddComponent<AIPlayer>();
vehicle.SetPlayer(ai);

player.destination = destination;
ai.destination = destination;

follow.target = vehicle.transform;
}
Expand Down
File renamed without changes.
73 changes: 73 additions & 0 deletions Assets/Adrenak/Tork/AI/Scripts/AIPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using UnityEngine;

namespace Adrenak.Tork {
[RequireComponent(typeof(Steering))]
public class AIPlayer : Player {
enum Direction {
Forward,
Reverse
}

public Transform destination;
Direction m_Direction = Direction.Forward;
Vehicle m_Vehicle;
Steering m_Steering;

bool m_IsInTurningCircle;

void Start() {
m_Vehicle = GetComponent<Vehicle>();
m_Steering = GetComponent<Steering>();
}

public override VehicleInput GetInput() {
var towards = destination.position - transform.position;
var locTowards = transform.InverseTransformDirection(towards);
var angle = Vector3.Angle(transform.forward, towards) * Mathf.Sign(locTowards.x);
bool isTargetOnRight = Mathf.Sign(locTowards.x) > 0;
bool isBehind = Vector3.Dot(towards, transform.forward) < 0;

// Get radii at the maximum steering angle
// This gives is the smallest turning radius the car can have
var radii = Ackermann.GetRadii(
m_Vehicle.GetMaxSteerAtSpeed(m_Vehicle.Velocity.magnitude),
m_Steering.Ackermann.AxleSeparation,
m_Steering.Ackermann.AxleWidth
);
var avgRadius = (radii[0, 0] + radii[0, 1] + radii[1, 0] + radii[1, 1]) / 4;
avgRadius = Mathf.Abs(avgRadius);

// Find out if the target is inside the turning circle
var pivot = m_Steering.Ackermann.GetPivot();
var localPivot = transform.InverseTransformPoint(pivot);
var isPivotOnRight = Mathf.Sign(localPivot.x) > 0;
// If the target and pivot are on opposite sides of the car
// we move the pivot along the local X axis so we can do a valid comparision
if (isPivotOnRight != isTargetOnRight)
localPivot.x *= -1;
pivot = transform.TransformPoint(localPivot);
var isInCircle = (destination.position - pivot).magnitude < avgRadius;

switch (m_Direction) {
case Direction.Forward:
Debug.DrawLine(transform.position, pivot, Color.green);
p_Input.acceleration = 1;
p_Input.brake = 0;
p_Input.steering = Mathf.Clamp(angle / m_Steering.range, -1, 1);
if (isBehind && isInCircle)
m_Direction = Direction.Reverse;
break;
case Direction.Reverse:
Debug.DrawLine(transform.position, pivot, Color.red);
p_Input.acceleration = -1;
p_Input.brake = 0;
p_Input.steering = Mathf.Clamp(angle / m_Steering.range, -1, 1) * (isTargetOnRight ? -1 : 1);
if (!isBehind && !isInCircle)
m_Direction = Direction.Forward;
break;
}

return p_Input;
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2663,7 +2663,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1da1bb9a22dc0004c803629affe1f6df, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Rigidbody: {fileID: 54767532765438496}
m_MotorTorqueVsSpeed:
serializedVersion: 2
m_Curve:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Adrenak.Tork {
[RequireComponent(typeof(Steering))]
[RequireComponent(typeof(Brakes))]
public class Vehicle : MonoBehaviour {
[SerializeField] Rigidbody m_Rigidbody;
public Vector3 Velocity { get { return m_Rigidbody.velocity; } }

[Tooltip("The maximum motor torque available based on the speed (KMPH)")]
[SerializeField] AnimationCurve m_MotorTorqueVsSpeed = AnimationCurve.Linear(0, 10000, 250, 0);
Expand All @@ -20,13 +20,15 @@ public class Vehicle : MonoBehaviour {
[Tooltip("The down force based on the speed (KMPH)")]
[SerializeField] AnimationCurve m_DownForceVsSpeed = AnimationCurve.Linear(0, 0, 250, 2500);

Rigidbody m_Rigidbody;
Steering m_Steering;
Motor m_Motor;
Brakes m_Brake;
Aerodynamics m_Aerodynamics;
Player m_Player;

void Start() {
m_Rigidbody = GetComponent<Rigidbody>();
m_Steering = GetComponent<Steering>();
m_Motor = GetComponent<Motor>();
m_Aerodynamics = GetComponent<Aerodynamics>();
Expand All @@ -40,11 +42,11 @@ public void SetPlayer(Player player) {
void Update() {
if (m_Player == null) return;

var speed = Vector3.Dot(m_Rigidbody.velocity, transform.forward) * 3.6F;
var speed = m_Rigidbody.velocity.magnitude * 3.6F;

m_Steering.range = m_MaxSteeringAngleVsSpeed.Evaluate(speed);
m_Motor.maxTorque = m_MotorTorqueVsSpeed.Evaluate(speed);
m_Aerodynamics.downForce = m_DownForceVsSpeed.Evaluate(speed);
m_Steering.range = GetMaxSteerAtSpeed(speed);
m_Motor.maxTorque = GetMotorTorqueAtSpeed(speed);
m_Aerodynamics.downForce = GetDownForceAtSpeed(speed);

var input = m_Player.GetInput();

Expand All @@ -53,5 +55,17 @@ void Update() {
m_Aerodynamics.midAirSteerInput = input.steering;
m_Brake.value = input.brake;
}

public float GetMotorTorqueAtSpeed(float speed) {
return m_MotorTorqueVsSpeed.Evaluate(speed);
}

public float GetMaxSteerAtSpeed(float speed) {
return m_MaxSteeringAngleVsSpeed.Evaluate(speed);
}

public float GetDownForceAtSpeed(float speed) {
return m_DownForceVsSpeed.Evaluate(speed);
}
}
}
9 changes: 0 additions & 9 deletions Assets/Plugins.meta

This file was deleted.

46 changes: 0 additions & 46 deletions Assets/Plugins/Adrenak/Tork/AI/Scripts/AIPlayer.cs

This file was deleted.

0 comments on commit 6d34f45

Please sign in to comment.