Skip to content

Commit

Permalink
Add Unity variants for everything
Browse files Browse the repository at this point in the history
  • Loading branch information
Vatsal Ambastha committed Sep 27, 2020
1 parent 58e944d commit 15154cd
Show file tree
Hide file tree
Showing 28 changed files with 4,682 additions and 119 deletions.
34 changes: 34 additions & 0 deletions Assets/Tork/Runtime/Addons/AntiRollUnity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Adrenak.Tork {
public class AntiRollUnity : VehicleAddOn {
[Serializable]
public class Axle {
public WheelCollider left;
public WheelCollider right;
public float force;
}

public new Rigidbody rigidbody;
public List<Axle> axles;

void FixedUpdate() {
foreach(var axle in axles) {
var wsDown = transform.TransformDirection(Vector3.down);
wsDown.Normalize();

float travelL = Mathf.Clamp01(axle.left.GetCompressionRatio()) ;
float travelR = Mathf.Clamp01(axle.right.GetCompressionRatio());
float antiRollForce = (travelL - travelR) * axle.force;

if (axle.left.isGrounded)
rigidbody.AddForceAtPosition(wsDown * -antiRollForce, axle.left.GetHit().point);

if (axle.right.isGrounded)
rigidbody.AddForceAtPosition(wsDown * antiRollForce, axle.right.GetHit().point);
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tork/Runtime/Addons/AntiRollUnity.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Assets/Tork/Runtime/Addons/AutoDrive.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using UnityEngine;

using UnityEngine;

namespace Adrenak.Tork {
public class AutoDrive : VehicleAddOn {
public Vehicle vehicle;
Expand Down
28 changes: 14 additions & 14 deletions Assets/Tork/Runtime/Addons/DownForceVsSpeed.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using UnityEngine;

namespace Adrenak.Tork {
public class DownForceVsSpeed : VehicleAddOn {
using UnityEngine;

namespace Adrenak.Tork {
public class DownForceVsSpeed : VehicleAddOn {
[Tooltip("The down force based on the speed (KMPH)")]
[SerializeField] AnimationCurve curve = AnimationCurve.Linear(0, 0, 250, 2500);

[SerializeField] new Rigidbody rigidbody;

[SerializeField] AnimationCurve curve = AnimationCurve.Linear(0, 0, 250, 2500);

[SerializeField] new Rigidbody rigidbody;

void Update() {
var downForce = GetDownForceAtSpeed(rigidbody.velocity.magnitude * 3.6f);
rigidbody.AddForce(-Vector3.up * downForce);
}

}

public float GetDownForceAtSpeed(float speed) {
return curve.Evaluate(speed);
}
}
}

}
}
}
14 changes: 7 additions & 7 deletions Assets/Tork/Runtime/Addons/MaxSteerAngleVsSpeed.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using UnityEngine;

namespace Adrenak.Tork {
namespace Adrenak.Tork {
public class MaxSteerAngleVsSpeed : VehicleAddOn {
[Tooltip("The steering angle based on the speed (KMPH)")]
[Tooltip("The steering angle based on the speed (KMPH)")]
[SerializeField] AnimationCurve curve = AnimationCurve.Linear(0, 35, 250, 5);

[SerializeField] Steering steering;
[SerializeField] new Rigidbody rigidbody;

void Update() {
steering.range = GetMaxSteerAtSpeed(rigidbody.velocity.magnitude * 3.6f);
void Update() {
steering.range = GetMaxSteerAtSpeed(rigidbody.velocity.magnitude * 3.6f);
}

public float GetMaxSteerAtSpeed(float speed) {
return curve.Evaluate(speed);
public float GetMaxSteerAtSpeed(float speed) {
return curve.Evaluate(speed);
}
}
}
}
32 changes: 16 additions & 16 deletions Assets/Tork/Runtime/Addons/MidAirStabilization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
using System.Collections.Generic;
using UnityEngine;

namespace Adrenak.Tork{
namespace Adrenak.Tork{
public class MidAirStabilization : VehicleAddOn {
[Header("Mid Air Stabilization")]
[Header("Mid Air Stabilization")]
public float stabilizationTorque = 15000;

public Rigidbody m_Rigidbody;
public TorkWheel[] m_Wheels;

void FixedUpdate() {
Stabilize();
void FixedUpdate() {
Stabilize();
}

void Stabilize() {
var inAir = m_Wheels.Where(x => x.IsGrounded);
if (inAir.Count() == 4) return;

// Try to keep vehicle parallel to the ground while jumping
Vector3 locUp = transform.up;
Vector3 wsUp = new Vector3(0.0f, 1.0f, 0.0f);
Vector3 axis = Vector3.Cross(locUp, wsUp);
float force = stabilizationTorque;

m_Rigidbody.AddTorque(axis * force);
void Stabilize() {
var inAir = m_Wheels.Where(x => x.IsGrounded);
if (inAir.Count() == 4) return;

// Try to keep vehicle parallel to the ground while jumping
Vector3 locUp = transform.up;
Vector3 wsUp = new Vector3(0.0f, 1.0f, 0.0f);
Vector3 axis = Vector3.Cross(locUp, wsUp);
float force = stabilizationTorque;

m_Rigidbody.AddTorque(axis * force);
}
}
}
}
18 changes: 9 additions & 9 deletions Assets/Tork/Runtime/Addons/MidAirSteering.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using UnityEngine;

using UnityEngine;

namespace Adrenak.Tork{
public class MidAirSteering : VehicleAddOn {
[Header("Mid Air Steer")]
public float midAirSteerTorque = 1500;
public float midAirSteerInput;
public Rigidbody m_Rigidbody;

public float midAirSteerInput;
public Rigidbody m_Rigidbody;

void FixedUpdate() {
SteerMidAir();
}

}

void SteerMidAir() {
if (!Mathf.Approximately(midAirSteerInput, 0))
m_Rigidbody.AddTorque(new Vector3(0, midAirSteerInput * midAirSteerTorque, 0));
}
}
}
}
}
21 changes: 8 additions & 13 deletions Assets/Tork/Runtime/Core/Ackermann.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Ackermann : MonoBehaviour {
[SerializeField] TorkWheel m_RearLeft;
public TorkWheel RearLeftWheel { get { return m_RearLeft; } }

public float Angle { get; set; }
public float angle;

public float AxleSeparation {
get { return (m_FrontLeft.transform.position - m_RearLeft.transform.position).magnitude; }
Expand All @@ -36,32 +36,27 @@ public float FrontLeftRadius {
}

void Update() {
var farAngle = AckermannUtils.GetSecondaryAngle(Angle, AxleSeparation, AxleWidth);
var farAngle = AckermannUtils.GetSecondaryAngle(angle, AxleSeparation, AxleWidth);

// The rear wheels are always at 0 steer in Ackermann
m_RearLeft.SteerAngle = m_RearRight.SteerAngle = 0;

if (Mathf.Approximately((float)Angle, 0))
if (Mathf.Approximately(angle, 0))
m_FrontRight.SteerAngle = m_FrontLeft.SteerAngle = 0;
else if (Angle > 0) {
m_FrontRight.SteerAngle = Angle;
m_FrontLeft.SteerAngle = farAngle;
}
else if (Angle < 0) {
m_FrontLeft.SteerAngle = Angle;
m_FrontRight.SteerAngle = -farAngle;
}

m_FrontLeft.SteerAngle = angle;
m_FrontRight.SteerAngle = farAngle;
}

public Vector3 GetPivot() {
if (Angle > 0)
if (angle > 0)
return m_RearRight.transform.position + CurrentRadii[0] * m_RearRight.transform.right;
else
return m_RearLeft.transform.position - CurrentRadii[0] * m_RearLeft.transform.right;
}

public float[] CurrentRadii {
get { return AckermannUtils.GetRadii(Angle, AxleSeparation, AxleWidth); }
get { return AckermannUtils.GetRadii(angle, AxleSeparation, AxleWidth); }
}
}
}
61 changes: 61 additions & 0 deletions Assets/Tork/Runtime/Core/AckermannUnity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Adrenak.Tork {
public class AckermannUnity : MonoBehaviour {
[SerializeField] WheelCollider m_FrontRight;
public WheelCollider FrontRightWheel { get { return m_FrontRight; } }

[SerializeField] WheelCollider m_FrontLeft;
public WheelCollider FrontLeftWheel { get { return m_FrontLeft; } }

[SerializeField] WheelCollider m_RearRight;
public WheelCollider RearRightWheel { get { return m_RearRight; } }

[SerializeField] WheelCollider m_RearLeft;
public WheelCollider RearLeftWheel { get { return m_RearLeft; } }

public float angle;

public float AxleSeparation {
get { return (m_FrontLeft.transform.position - m_RearLeft.transform.position).magnitude; }
}

public float AxleWidth {
get { return (m_FrontLeft.transform.position - m_FrontRight.transform.position).magnitude; }
}

public float FrontRightRadius {
get { return AxleSeparation / Mathf.Sin(Mathf.Abs(m_FrontRight.steerAngle)); }
}

public float FrontLeftRadius {
get { return AxleSeparation / Mathf.Sin(Mathf.Abs(m_FrontLeft.steerAngle)); }
}

void Update() {
var farAngle = AckermannUtils.GetSecondaryAngle(angle, AxleSeparation, AxleWidth);

// The rear wheels are always at 0 steer in Ackermann
m_RearLeft.steerAngle = m_RearRight.steerAngle = 0;

if (Mathf.Approximately(angle, 0))
m_FrontRight.steerAngle = m_FrontLeft.steerAngle = 0;

m_FrontLeft.steerAngle = angle;
m_FrontRight.steerAngle = farAngle;
}

public Vector3 GetPivot() {
if (angle > 0)
return m_RearRight.transform.position + CurrentRadii[0] * m_RearRight.transform.right;
else
return m_RearLeft.transform.position - CurrentRadii[0] * m_RearLeft.transform.right;
}

public float[] CurrentRadii {
get { return AckermannUtils.GetRadii(angle, AxleSeparation, AxleWidth); }
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tork/Runtime/Core/AckermannUnity.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Assets/Tork/Runtime/Core/Brakes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ void FixedUpdate() {
float fs, fp, rs, rp;

// If we have Ackerman steering, we apply torque based on the steering radius of each wheel
var radii = AckermannUtils.GetRadii(ackermann.Angle, ackermann.AxleSeparation, ackermann.AxleWidth);
var radii = AckermannUtils.GetRadii(ackermann.angle, ackermann.AxleSeparation, ackermann.AxleWidth);
var total = radii[0] + radii[1] + radii[2] + radii[3];
fp = radii[0] / total;
fs = radii[1] / total;
rp = radii[2] / total;
rs = radii[3] / total;

if (ackermann.Angle > 0) {
if (ackermann.angle > 0) {
ackermann.FrontRightWheel.BrakeTorque = value * maxTorque * fp;
ackermann.FrontLeftWheel.BrakeTorque = value * maxTorque * fs;
ackermann.RearRightWheel.BrakeTorque = value * maxTorque * rp;
Expand Down
40 changes: 40 additions & 0 deletions Assets/Tork/Runtime/Core/BrakesUnity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using UnityEngine;

namespace Adrenak.Tork {
public class BrakesUnity : MonoBehaviour {
public AckermannUnity ackermann;

[Tooltip("The maximum braking torque that can be applied")]
[SerializeField] float maxTorque = 5000;

[Tooltip("Multiplier to the maxTorque [0..1]")]
public float value;

void FixedUpdate() {
value = Mathf.Clamp01(value);

float fs, fp, rs, rp;

// If we have Ackerman steering, we apply torque based on the steering radius of each wheel
var radii = AckermannUtils.GetRadii(ackermann.angle, ackermann.AxleSeparation, ackermann.AxleWidth);
var total = radii[0] + radii[1] + radii[2] + radii[3];
fp = radii[0] / total;
fs = radii[1] / total;
rp = radii[2] / total;
rs = radii[3] / total;

if (ackermann.angle > 0) {
ackermann.FrontRightWheel.brakeTorque = value * maxTorque * fp;
ackermann.FrontLeftWheel.brakeTorque = value * maxTorque * fs;
ackermann.RearRightWheel.brakeTorque = value * maxTorque * rp;
ackermann.RearLeftWheel.brakeTorque = value * maxTorque * rs;
}
else {
ackermann.FrontLeftWheel.brakeTorque = value * maxTorque * fp;
ackermann.FrontRightWheel.brakeTorque = value * maxTorque * fs;
ackermann.RearLeftWheel.brakeTorque = value * maxTorque * rp;
ackermann.RearRightWheel.brakeTorque = value * maxTorque * rs;
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tork/Runtime/Core/BrakesUnity.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 15154cd

Please sign in to comment.