From dbed6e171d6dafb909dce9f7ed157141a36768fe Mon Sep 17 00:00:00 2001 From: Vatsal Ambastha Date: Mon, 27 Apr 2020 01:25:22 +0530 Subject: [PATCH] feat: Add AutoDrive addon Ackermann code refactored and cleaned up. Classes using Ackermann have undergone changes --- Assets/Tork/Runtime/Addons/AutoDrive.cs | 86 +++++++++++------- Assets/Tork/Runtime/Core/Ackermann.cs | 69 ++------------ Assets/Tork/Runtime/Core/Brakes.cs | 50 ++++++----- Assets/Tork/Runtime/Core/Motor.cs | 66 ++++++++------ Assets/Tork/Runtime/Core/Steering.cs | 2 +- Assets/Tork/Runtime/Core/TorkWheel.cs | 10 +-- Assets/Tork/Runtime/Utils/AckermanUtils.cs | 63 +++++++++++++ .../Tork/Runtime/Utils/AckermanUtils.cs.meta | 11 +++ Assets/Tork/Samples/Prefabs/Tocus.prefab | 53 ++++++++++- Assets/Tork/Samples/Scenes/Demo.cs | 20 +++-- Assets/Tork/Samples/Scenes/Demo.unity | 90 ++----------------- 11 files changed, 280 insertions(+), 240 deletions(-) create mode 100644 Assets/Tork/Runtime/Utils/AckermanUtils.cs create mode 100644 Assets/Tork/Runtime/Utils/AckermanUtils.cs.meta diff --git a/Assets/Tork/Runtime/Addons/AutoDrive.cs b/Assets/Tork/Runtime/Addons/AutoDrive.cs index 4005810..92abc6b 100644 --- a/Assets/Tork/Runtime/Addons/AutoDrive.cs +++ b/Assets/Tork/Runtime/Addons/AutoDrive.cs @@ -3,49 +3,75 @@ 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 Vector3 destination; + public float steeringRate = .5f; + public float brakingDistance; - public bool IsInReverseArea { + public float Direction { get { - var towards = destination.position - transform.position; - var locTowards = transform.InverseTransformDirection(towards); + var towards = destination - vehicle.transform.position; + var locTowards = vehicle.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; + // Whether the current destination is behind us + bool destinationIsBehind = locTowards.z < 0; - if (Vector3.Distance(pivot, destination.position) < currMinRadius) - return true; + // The minimum turning radius of the vehicle at the given steering angle range + var minTurningDia = AckermannUtils.GetRadius( + vehicle.Steering.range, + vehicle.Ackermann.AxleSeparation, + vehicle.Ackermann.AxleWidth + ) * 2; - bool isBehind = locTowards.z < 0; - if (isBehind && Vector3.Distance(pivot, destination.position) < currMinRadius * 2) - return true; + // The center of the vehicle if it turns at the minimum turning radius right now + var minTurningRadiusCenter = vehicle.transform.position + vehicle.transform.right * Mathf.Sign(locTowards.x) * minTurningDia / 2; - return false; + // We have the min turning radius, but we create a "band" of turnign radius + // with an inner and an outer radii + float innerDia = minTurningDia - minTurningDia / 4; + float outerDia = minTurningDia + minTurningDia / 4; + + // x is the distance between the center about which we will move if we decide to move + // with the smallest turning radius and the destination + float x = Vector3.Distance(minTurningRadiusCenter, destination); + + // We certainly need to reverse as the inner radius is significantly smaller + // than the actual min turnign radius itself and we will certainly not make it + // at min radius + if (x < innerDia / 2) + return -1; + + // We certainly can go in the forward direction because the outer radius + // is significantly larger than the actual min turning radius itself and we will + // certainly make it at the min radius + else if (x > outerDia / 2) + return 1; + + // + else if (destinationIsBehind && x < innerDia) + return -1; + + else if (destinationIsBehind && x > outerDia) + return 1; + + else if (x < minTurningDia) + return -(minTurningDia - x) / (minTurningDia - innerDia); + else + return (x - minTurningDia) / (outerDia - minTurningDia); } } void Update() { - var distance = Vector3.Distance(vehicle.transform.position, destination.position); - - var towards = destination.position - transform.position; + var distance = Vector3.Distance(vehicle.transform.position, destination); + + var towards = destination - 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); + var direction = Direction; + var multiplier = Mathf.Clamp01(distance / brakingDistance); + vehicle.Brake.value = 1 - multiplier; + vehicle.Motor.value = direction; + vehicle.Steering.Angle = Mathf.Lerp(vehicle.Steering.Angle, reqAngle, steeringRate) * direction; } } } \ No newline at end of file diff --git a/Assets/Tork/Runtime/Core/Ackermann.cs b/Assets/Tork/Runtime/Core/Ackermann.cs index 89ebdfa..02e7ed8 100644 --- a/Assets/Tork/Runtime/Core/Ackermann.cs +++ b/Assets/Tork/Runtime/Core/Ackermann.cs @@ -17,9 +17,7 @@ public class Ackermann : MonoBehaviour { [SerializeField] TorkWheel m_RearLeft; public TorkWheel RearLeftWheel { get { return m_RearLeft; } } - public float[,] Radii { get; private set; } - - public float Angle { get; private set; } + public float Angle { get; set; } public float AxleSeparation { get { return (m_FrontLeft.transform.position - m_RearLeft.transform.position).magnitude; } @@ -37,22 +35,13 @@ public float FrontLeftRadius { get { return AxleSeparation / Mathf.Sin(Mathf.Abs(m_FrontLeft.SteerAngle)); } } - private void Start() { - Radii = new float[2, 2]; - } - - private void Update() { - Radii = GetRadii(Angle, AxleSeparation, AxleWidth); - } - - public void SetAngle(float angle) { - Angle = angle; - var farAngle = GetSecondaryAngle(angle, AxleWidth, AxleSeparation); + 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)) + if (Mathf.Approximately((float)Angle, 0)) m_FrontRight.SteerAngle = m_FrontLeft.SteerAngle = 0; else if (Angle > 0) { m_FrontRight.SteerAngle = Angle; @@ -66,55 +55,13 @@ public void SetAngle(float angle) { public Vector3 GetPivot() { if (Angle > 0) - return m_FrontRight.transform.position + Radii[0, 1] * m_FrontRight.transform.right; + return m_RearRight.transform.position + CurrentRadii[0] * m_RearRight.transform.right; else - return m_FrontLeft.transform.position - Radii[0, 0] * m_FrontLeft.transform.right; - } - - public float[,] CurrentRadii { - get { return GetRadii(Angle, AxleSeparation, AxleWidth); } - } - - public float CurrentTurningRadius { - get { - var radii = CurrentRadii; - return (radii[0, 0] + radii[0, 1]) / 2; - } + return m_RearLeft.transform.position - CurrentRadii[0] * m_RearLeft.transform.right; } - public static float GetSecondaryAngle(float angle, float width, float separation) { - float close = separation / Mathf.Tan(Mathf.Abs(angle) * Mathf.Deg2Rad); - float far = close + width; - - return Mathf.Atan(separation / far) * Mathf.Rad2Deg; - } - - public static float GetRadius(float angle, float separation, float width){ - var radii = GetRadii(angle, separation, width); - return radii[0, 0] + radii[0, 1] + radii[1, 0] + radii[1, 1] / 4; - } - - public static float[,] GetRadii(float angle, float separation, float width) { - var secAngle = GetSecondaryAngle(angle, width, separation); - float[,] radii = new float[2, 2]; - - if (Mathf.Abs(angle) < 1) - radii[0, 0] = radii[1, 0] = radii[0, 1] = radii[1, 1] = 1000; - - if (angle < -1) { - radii[0, 0] = separation / Mathf.Sin(Mathf.Abs(angle * Mathf.Deg2Rad)); - radii[0, 1] = separation / Mathf.Sin(Mathf.Abs(secAngle * Mathf.Deg2Rad)); - radii[1, 0] = separation / Mathf.Tan(Mathf.Abs(angle * Mathf.Deg2Rad)); - radii[1, 1] = separation / Mathf.Tan(Mathf.Abs(secAngle * Mathf.Deg2Rad)); - } - else if (angle > 1) { - radii[0, 0] = separation / Mathf.Sin(Mathf.Abs(secAngle * Mathf.Deg2Rad)); - radii[0, 1] = separation / Mathf.Sin(Mathf.Abs(angle * Mathf.Deg2Rad)); - radii[1, 0] = separation / Mathf.Tan(Mathf.Abs(secAngle * Mathf.Deg2Rad)); - radii[1, 1] = separation / Mathf.Tan(Mathf.Abs(angle * Mathf.Deg2Rad)); - } - - return radii; + public float[] CurrentRadii { + get { return AckermannUtils.GetRadii(Angle, AxleSeparation, AxleWidth); } } } } diff --git a/Assets/Tork/Runtime/Core/Brakes.cs b/Assets/Tork/Runtime/Core/Brakes.cs index 2bf5727..c6fffb9 100644 --- a/Assets/Tork/Runtime/Core/Brakes.cs +++ b/Assets/Tork/Runtime/Core/Brakes.cs @@ -1,36 +1,40 @@ using UnityEngine; namespace Adrenak.Tork { - public class Brakes : MonoBehaviour { + public class Brakes : MonoBehaviour { public Ackermann ackermann; - [Tooltip("The maximum braking torque that can be applied")] - [SerializeField] float maxTorque = 5000; + [Tooltip("The maximum braking torque that can be applied")] + [SerializeField] float maxTorque = 5000; - [Tooltip("Multiplier to the maxTorque [0..1]")] - public float value; + [Tooltip("Multiplier to the maxTorque [0..1]")] + public float value; - void FixedUpdate() { + void FixedUpdate() { value = Mathf.Clamp01(value); - float fr, fl, rr, rl; + float fs, fp, rs, rp; // If we have Ackerman steering, we apply torque based on the steering radius of each wheel - if (ackermann != null) { - var radii = Ackermann.GetRadii(ackermann.Angle, ackermann.AxleSeparation, ackermann.AxleWidth); - var total = radii[0, 0] + radii[1, 0] + radii[0, 1] + radii[1, 1]; - fl = radii[0, 0] / total; - fr = radii[1, 0] / total; - rl = radii[0, 1] / total; - rr = radii[1, 1] / total; - } - else - fr = fl = rr = rl = .25f; + 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; - ackermann.FrontLeftWheel.BrakeTorque = value * maxTorque * fl; - ackermann.FrontRightWheel.BrakeTorque = value * maxTorque * fr; - ackermann.RearLeftWheel.BrakeTorque = value * maxTorque * rl; - ackermann.RearRightWheel.BrakeTorque = value * maxTorque * rr; - } - } + 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; + } + } + } } \ No newline at end of file diff --git a/Assets/Tork/Runtime/Core/Motor.cs b/Assets/Tork/Runtime/Core/Motor.cs index 49ea2a7..c03d39a 100644 --- a/Assets/Tork/Runtime/Core/Motor.cs +++ b/Assets/Tork/Runtime/Core/Motor.cs @@ -2,36 +2,48 @@ using UnityEngine; namespace Adrenak.Tork { - public class Motor : MonoBehaviour { - [Tooltip("The maximum torque that the motor generates")] - public float maxTorque = 10000; + public class Motor : MonoBehaviour { + [Tooltip("The maximum torque that the motor generates")] + public float maxTorque = 10000; - [Tooltip("Multiplier to the maxTorque")] - public float value; + [Tooltip("Multiplier to the maxTorque")] + public float value; - public float m_MaxReverseInput = -.5f; + public float m_MaxReverseInput = -.5f; public Ackermann ackermann; - void FixedUpdate() { - ApplyMotorTorque(); - } - - void ApplyMotorTorque() { - value = Mathf.Clamp(value, m_MaxReverseInput, 1); - - // If we have Ackerman steering, we apply torque based on the steering radius of each wheel - var radii = Ackermann.GetRadii(ackermann.Angle, ackermann.AxleSeparation, ackermann.AxleWidth); - var total = radii[0, 0] + radii[1, 0] + radii[0, 1] + radii[1, 1]; - var fl = radii[0, 0] / total; - var fr = radii[1, 0] / total; - var rl = radii[0, 1] / total; - var rr = radii[1, 1] / total; - - ackermann.FrontLeftWheel.MotorTorque = value * maxTorque * fl; - ackermann.FrontRightWheel.MotorTorque = value * maxTorque * fr; - ackermann.RearLeftWheel.MotorTorque = value * maxTorque * rl; - ackermann.RearRightWheel.MotorTorque = value * maxTorque * rr; - } - } + void FixedUpdate() { + ApplyMotorTorque(); + } + + void Update() { + value = Mathf.Clamp(value, m_MaxReverseInput, 1); + } + + void ApplyMotorTorque() { + 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.MotorTorque = value * maxTorque * fp; + ackermann.FrontLeftWheel.MotorTorque = value * maxTorque * fs; + ackermann.RearRightWheel.MotorTorque = value * maxTorque * rp; + ackermann.RearLeftWheel.MotorTorque = value * maxTorque * rs; + } + else { + ackermann.FrontLeftWheel.MotorTorque = value * maxTorque * fp; + ackermann.FrontRightWheel.MotorTorque = value * maxTorque * fs; + ackermann.RearLeftWheel.MotorTorque = value * maxTorque * rp; + ackermann.RearRightWheel.MotorTorque = value * maxTorque * rs; + } + } + } } diff --git a/Assets/Tork/Runtime/Core/Steering.cs b/Assets/Tork/Runtime/Core/Steering.cs index 7976938..5a04c00 100644 --- a/Assets/Tork/Runtime/Core/Steering.cs +++ b/Assets/Tork/Runtime/Core/Steering.cs @@ -20,7 +20,7 @@ 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); + ackermann.Angle = m_CurrAngle; } } } diff --git a/Assets/Tork/Runtime/Core/TorkWheel.cs b/Assets/Tork/Runtime/Core/TorkWheel.cs index 599cefe..0e3335d 100644 --- a/Assets/Tork/Runtime/Core/TorkWheel.cs +++ b/Assets/Tork/Runtime/Core/TorkWheel.cs @@ -136,6 +136,8 @@ void Start() { } void FixedUpdate() { + Velocity = rigidbody.GetPointVelocity(Hit.point); + transform.localEulerAngles = new Vector3(0, SteerAngle, 0); CalculateSuspension(); CalculateFriction(); @@ -198,10 +200,9 @@ bool WheelRaycast(float maxDistance, ref RaycastHit nearestHit) { } void CalculateFriction() { - Velocity = rigidbody.GetPointVelocity(Hit.point); - if (!IsGrounded) return; + // Contact basis (can be different from wheel basis) Vector3 normal = Hit.normal; Vector3 side = transform.right; @@ -212,8 +213,8 @@ void CalculateFriction() { var multiplier = Mathf.Cos(angle * Mathf.Deg2Rad); // Calculate sliding velocity (velocity without normal force) - Vector3 sideVel = Vector3.Dot(Velocity, side) * side * multiplier; - Vector3 forwardVel = Vector3.Dot(Velocity, forward) * forward * multiplier; + Vector3 sideVel = side * Vector3.Dot(Velocity, side) * multiplier; + Vector3 forwardVel = forward * Vector3.Dot(Velocity, forward) * multiplier; Vector3 planarVelocity2D = sideVel + forwardVel; Vector3 planarMomentum = planarVelocity2D * rigidbody.mass; @@ -236,7 +237,6 @@ void CalculateFriction() { longForce -= brakeResistanceForce; gripResistanceForce -= longForce; - rigidbody.AddForceAtPosition(gripResistanceForce, Hit.point); rigidbody.AddForceAtPosition(forward * MotorTorque / radius * forwardGrip * engineShaftToWheelRatio, Hit.point); } diff --git a/Assets/Tork/Runtime/Utils/AckermanUtils.cs b/Assets/Tork/Runtime/Utils/AckermanUtils.cs new file mode 100644 index 0000000..4fe187a --- /dev/null +++ b/Assets/Tork/Runtime/Utils/AckermanUtils.cs @@ -0,0 +1,63 @@ +using UnityEngine; + +namespace Adrenak.Tork { + public static class AckermannUtils { + /// + /// Returns current turning radius of secondary wheel + /// + /// Angle the inner front wheel is making + /// Distance between front and rear wheels + /// Distance between left and right wheels + /// + public static float GetSecondaryAngle(float primaryAngle, float separation, float width) { + // To avoid NaN we assume primaryAngle to be at least 1 + primaryAngle = Mathf.Clamp(primaryAngle, 1, Mathf.Infinity); + float close = separation / Mathf.Tan(Mathf.Abs(primaryAngle) * Mathf.Deg2Rad); + float far = close + width; + return Mathf.Atan(separation / far) * Mathf.Rad2Deg; + } + + /// + /// Returns the current turning redius of each wheel + /// + /// Primary front angle. IE. if turning right, angle of front right wheel + /// Distance between front and rear wheels + /// Distance between left and right wheels + /// + public static float[] GetRadii(float primaryAngle, float separation, float width) { + // To avoid NaN we assume primaryAngle to be at least 1 + primaryAngle = Mathf.Clamp(primaryAngle, 1, Mathf.Infinity); + + var frontPrimary = separation / Mathf.Sin(primaryAngle * Mathf.Deg2Rad); + var rearPrimary = separation / Mathf.Tan(primaryAngle * Mathf.Deg2Rad); + var rearSecondary = width + rearPrimary; + var frontSecondary = Mathf.Sqrt(separation * separation + rearSecondary * rearSecondary); + + return new[] { + frontPrimary, + frontSecondary, + rearPrimary, + rearSecondary + }; + } + + /// + /// Returns current average turning radius of the wheels + /// + /// Primary front angle. IE. if turning right, angle of front right wheel + /// Distance between front and rear wheels + /// Distance between left and right wheels + /// + public static float GetRadius(float primaryAngle, float separation, float width){ + // To avoid NaN we assume primaryAngle to be at least 1 + primaryAngle = Mathf.Clamp(primaryAngle, 1, Mathf.Infinity); + var radii = GetRadii(primaryAngle, separation, width); + float sum = 0; + + for (int i = 0; i < radii.Length; i++) + sum += radii[i]; + + return sum / 4; + } + } +} diff --git a/Assets/Tork/Runtime/Utils/AckermanUtils.cs.meta b/Assets/Tork/Runtime/Utils/AckermanUtils.cs.meta new file mode 100644 index 0000000..19f6867 --- /dev/null +++ b/Assets/Tork/Runtime/Utils/AckermanUtils.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9a6814d8b654a1245b5e3a07573cc4a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tork/Samples/Prefabs/Tocus.prefab b/Assets/Tork/Samples/Prefabs/Tocus.prefab index 166c428..b710c2c 100644 --- a/Assets/Tork/Samples/Prefabs/Tocus.prefab +++ b/Assets/Tork/Samples/Prefabs/Tocus.prefab @@ -1,5 +1,52 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: +--- !u!1 &255152389 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 255152390} + - component: {fileID: 255152392} + m_Layer: 0 + m_Name: Auto Drive + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &255152390 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 255152389} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 2425653309673728414} + m_RootOrder: 7 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &255152392 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 255152389} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 04ba35be00207f1408fcd9d4f9881aaf, type: 3} + m_Name: + m_EditorClassIdentifier: + vehicle: {fileID: 114480416164222720} + destination: {x: 0, y: 0, z: 0} + steeringRate: 0.5 + brakingDistance: 10 --- !u!1 &274411936 GameObject: m_ObjectHideFlags: 0 @@ -640,6 +687,7 @@ MonoBehaviour: - {fileID: 1214198106691927564} - {fileID: 1530015582167757396} - {fileID: 4502441065919909737} + - {fileID: 255152392} --- !u!1 &1166002035971444 GameObject: m_ObjectHideFlags: 0 @@ -2931,7 +2979,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: ackermann: {fileID: 1214198106228332029} - maxTorque: 4750 + maxTorque: 1000 value: 0 --- !u!1 &1214198106228332026 GameObject: @@ -3111,7 +3159,7 @@ MonoBehaviour: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1214198106691927562} - m_Enabled: 1 + m_Enabled: 0 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: 3ea26387ab63fdc4088fabfe3a742a8e, type: 3} m_Name: @@ -3612,6 +3660,7 @@ Transform: - {fileID: 1214198107628456251} - {fileID: 1214198106691927565} - {fileID: 2895941578727108575} + - {fileID: 255152390} m_Father: {fileID: 8897573135319807936} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Assets/Tork/Samples/Scenes/Demo.cs b/Assets/Tork/Samples/Scenes/Demo.cs index 683374c..0629f04 100644 --- a/Assets/Tork/Samples/Scenes/Demo.cs +++ b/Assets/Tork/Samples/Scenes/Demo.cs @@ -6,6 +6,9 @@ public class Demo : MonoBehaviour { [Header("Common")] public Vehicle vehicle; public SmoothFollow smoothFollow; + public Transform destination; + + public bool autoDrive; IVehicleDriver driver; MidAirStabilization midAirStabilization; @@ -13,16 +16,21 @@ public class Demo : MonoBehaviour { void Start() { midAirStabilization = vehicle.GetAddOn(); - driver = new KeyboardVehicleDriver(); - driver.RegisterVehicle(vehicle); + if (!autoDrive) { + driver = new KeyboardVehicleDriver(); + driver.RegisterVehicle(vehicle); + } + else + vehicle.GetAddOn().enabled = true; + smoothFollow.target = vehicle.transform; } void Update() { - //driver.DriveVehicles(); - - if (Input.GetKeyDown(KeyCode.R)) - midAirStabilization.enabled = !midAirStabilization.enabled; + if (!autoDrive) + driver.DriveVehicles(); + else + vehicle.GetAddOn().destination = destination.position; } void OnGUI() { diff --git a/Assets/Tork/Samples/Scenes/Demo.unity b/Assets/Tork/Samples/Scenes/Demo.unity index a684ad0..2b51189 100644 --- a/Assets/Tork/Samples/Scenes/Demo.unity +++ b/Assets/Tork/Samples/Scenes/Demo.unity @@ -387,77 +387,6 @@ MeshFilter: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 194512612} m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} ---- !u!1 &255152389 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 255152390} - - component: {fileID: 255152392} - m_Layer: 0 - m_Name: Auto Drive - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!4 &255152390 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 255152389} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} - m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: [] - m_Father: {fileID: 1553216374} - m_RootOrder: 7 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &255152392 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 255152389} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 04ba35be00207f1408fcd9d4f9881aaf, type: 3} - m_Name: - m_EditorClassIdentifier: - vehicle: {fileID: 1151711987} - destination: {fileID: 595338423} - rate: 0.5 - minDistance: 5 - accelerationVsDist: - serializedVersion: 2 - m_Curve: - - serializedVersion: 3 - time: 0 - value: 0.5 - inSlope: 0 - outSlope: 0.01 - tangentMode: 0 - weightedMode: 0 - inWeight: 0 - outWeight: 0 - - serializedVersion: 3 - time: 50 - value: 1 - inSlope: 0.01 - outSlope: 0 - tangentMode: 0 - weightedMode: 0 - inWeight: 0 - outWeight: 0 - m_PreInfinity: 2 - m_PostInfinity: 2 - m_RotationOrder: 4 --- !u!1 &282068337 GameObject: m_ObjectHideFlags: 0 @@ -3292,12 +3221,6 @@ MeshFilter: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1506344195} m_Mesh: {fileID: 10206, guid: 0000000000000000e000000000000000, type: 0} ---- !u!4 &1553216374 stripped -Transform: - m_CorrespondingSourceObject: {fileID: 2425653309673728414, guid: 58b5a3e507c7b68449674674e18cab44, - type: 3} - m_PrefabInstance: {fileID: 2072883566} - m_PrefabAsset: {fileID: 0} --- !u!1 &1602958346 GameObject: m_ObjectHideFlags: 0 @@ -4085,6 +4008,8 @@ MonoBehaviour: m_EditorClassIdentifier: vehicle: {fileID: 1151711987} smoothFollow: {fileID: 1853051912} + destination: {fileID: 595338423} + autoDrive: 1 --- !u!4 &2016063157 Transform: m_ObjectHideFlags: 0 @@ -4110,22 +4035,17 @@ PrefabInstance: propertyPath: m_Name value: Tocus objectReference: {fileID: 0} - - target: {fileID: 1214198106157320312, guid: 58b5a3e507c7b68449674674e18cab44, - type: 3} - propertyPath: maxTorque - value: 1000 - objectReference: {fileID: 0} - target: {fileID: 4261489924384528, guid: 58b5a3e507c7b68449674674e18cab44, type: 3} propertyPath: m_LocalPosition.x - value: 18.88 + value: 59.5 objectReference: {fileID: 0} - target: {fileID: 4261489924384528, guid: 58b5a3e507c7b68449674674e18cab44, type: 3} propertyPath: m_LocalPosition.y - value: 1.5 + value: 0.6 objectReference: {fileID: 0} - target: {fileID: 4261489924384528, guid: 58b5a3e507c7b68449674674e18cab44, type: 3} propertyPath: m_LocalPosition.z - value: 44.7 + value: 63.8 objectReference: {fileID: 0} - target: {fileID: 4261489924384528, guid: 58b5a3e507c7b68449674674e18cab44, type: 3} propertyPath: m_LocalRotation.x