Skip to content

Commit

Permalink
Add damping to AIPlayer steering
Browse files Browse the repository at this point in the history
  • Loading branch information
adrenak committed Jul 8, 2019
1 parent 6d34f45 commit da814b5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Assets/Adrenak/Tork/AI/Demo/AIDemo.unity
Original file line number Diff line number Diff line change
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: 17.84, y: 0, z: 50.68}
m_LocalPosition: {x: 22.45, y: 1.6846727, z: 16.56}
m_LocalScale: {x: 2.254064, y: 2.2540638, z: 2.2540638}
m_Children: []
m_Father: {fileID: 0}
Expand Down
1 change: 1 addition & 0 deletions Assets/Adrenak/Tork/AI/Demo/Scripts/AIDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void Start() {
vehicle.SetPlayer(ai);

ai.destination = destination;
ai.steerDamping = .5f;

follow.target = vehicle.transform;
}
Expand Down
18 changes: 14 additions & 4 deletions Assets/Adrenak/Tork/AI/Scripts/AIPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
namespace Adrenak.Tork {
[RequireComponent(typeof(Steering))]
public class AIPlayer : Player {
enum Direction {
public enum Direction {
Forward,
Reverse
}

public Transform destination;
Direction m_Direction = Direction.Forward;
public Direction m_Direction = Direction.Forward;
public float steerDamping = .5f;

Vehicle m_Vehicle;
Steering m_Steering;
float lastSteer;

bool m_IsInTurningCircle;

Expand All @@ -20,12 +23,15 @@ void Start() {
m_Steering = GetComponent<Steering>();
}

public bool isBehind;
public bool isInCircle;

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;
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
Expand All @@ -46,14 +52,15 @@ public override VehicleInput GetInput() {
if (isPivotOnRight != isTargetOnRight)
localPivot.x *= -1;
pivot = transform.TransformPoint(localPivot);
var isInCircle = (destination.position - pivot).magnitude < avgRadius;
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);
lastSteer = p_Input.steering;
if (isBehind && isInCircle)
m_Direction = Direction.Reverse;
break;
Expand All @@ -62,6 +69,9 @@ public override VehicleInput GetInput() {
p_Input.acceleration = -1;
p_Input.brake = 0;
p_Input.steering = Mathf.Clamp(angle / m_Steering.range, -1, 1) * (isTargetOnRight ? -1 : 1);
lastSteer = p_Input.steering;


if (!isBehind && !isInCircle)
m_Direction = Direction.Forward;
break;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Adrenak/Tork/Physics/Scripts/Vehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ void Update() {

m_Steering.value = input.steering;
m_Motor.value = input.acceleration;
m_Aerodynamics.midAirSteerInput = input.steering;
m_Brake.value = input.brake;
m_Aerodynamics.midAirSteerInput = input.steering;
}

public float GetMotorTorqueAtSpeed(float speed) {
Expand Down

0 comments on commit da814b5

Please sign in to comment.