-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vatsal Ambastha
committed
Apr 26, 2020
1 parent
baff332
commit 22bb006
Showing
10 changed files
with
255 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using UnityEngine; | ||
|
||
namespace Adrenak.Tork { | ||
public class AutoDrive : VehicleAddOn { | ||
public Vehicle vehicle; | ||
public Transform destination; | ||
public float rate = .5f; | ||
public float minDistance; | ||
public AnimationCurve accelerationVsDist = AnimationCurve.Linear(0, .5f, 50, 1f); | ||
|
||
public bool IsInReverseArea { | ||
get { | ||
var towards = destination.position - transform.position; | ||
var locTowards = transform.InverseTransformDirection(towards); | ||
|
||
var currMaxSteerAngle = Mathf.Abs(vehicle.Steering.range); | ||
var separation = vehicle.Ackermann.AxleSeparation; | ||
var width = vehicle.Ackermann.AxleWidth; | ||
var currMinRadius = Ackermann.GetRadius(currMaxSteerAngle, separation, width); | ||
var pivot = vehicle.transform.position + vehicle.transform.right * Mathf.Sign(locTowards.x) * currMinRadius; | ||
|
||
if (Vector3.Distance(pivot, destination.position) < currMinRadius) | ||
return true; | ||
|
||
bool isBehind = locTowards.z < 0; | ||
if (isBehind && Vector3.Distance(pivot, destination.position) < currMinRadius * 2) | ||
return true; | ||
|
||
return false; | ||
} | ||
} | ||
|
||
void Update() { | ||
var distance = Vector3.Distance(vehicle.transform.position, destination.position); | ||
|
||
var towards = destination.position - transform.position; | ||
var locTowards = transform.InverseTransformDirection(towards); | ||
var reqAngle = Vector3.Angle(transform.forward, towards) * Mathf.Sign(locTowards.x); | ||
|
||
var isAhead = locTowards.z > 0; | ||
|
||
if (isAhead) | ||
vehicle.Brake.value = 1 - (distance / minDistance); | ||
else | ||
vehicle.Brake.value = 0; | ||
|
||
vehicle.Motor.value = accelerationVsDist.Evaluate(distance) * (IsInReverseArea ? -1 : 1); | ||
vehicle.Steering.Angle = Mathf.Lerp(vehicle.Steering.Angle, reqAngle, rate) * (IsInReverseArea ? -1 : 1); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
using UnityEngine; | ||
|
||
namespace Adrenak.Tork { | ||
public class Steering : MonoBehaviour { | ||
public float range = 35; | ||
public float value; // 0..1 | ||
public float rate = 45; | ||
public class Steering : MonoBehaviour { | ||
public float range = 35; | ||
public float value; // 0..1 | ||
public float rate = 45; | ||
|
||
public Ackermann ackermann; | ||
float m_CurrAngle; | ||
float m_CurrAngle; | ||
|
||
void Update() { | ||
var destination = value * range; | ||
m_CurrAngle = Mathf.MoveTowards(m_CurrAngle, destination, Time.deltaTime * rate); | ||
m_CurrAngle = Mathf.Clamp(m_CurrAngle, -range, range); | ||
ackermann.SetAngle(m_CurrAngle); | ||
} | ||
} | ||
public float Angle { | ||
get { return range * value; } | ||
set { | ||
this.value = value / range; | ||
} | ||
} | ||
|
||
void Update() { | ||
var destination = value * range; | ||
m_CurrAngle = Mathf.MoveTowards(m_CurrAngle, destination, Time.deltaTime * rate); | ||
m_CurrAngle = Mathf.Clamp(m_CurrAngle, -range, range); | ||
ackermann.SetAngle(m_CurrAngle); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters