108 lines
5.3 KiB
C#
108 lines
5.3 KiB
C#
#if UNITY_5_3_OR_NEWER
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
namespace RoboidControl.Unity {
|
|
public class DifferentialDrive : Thing {
|
|
|
|
public WheelCollider leftWheel;
|
|
public WheelCollider rightWheel;
|
|
|
|
/// <summary>
|
|
/// Create the Unity representation
|
|
/// </summary>
|
|
/// <param name="core">The core touch sensor</param>
|
|
/// <returns>The Unity representation of the touch sensor</returns>
|
|
public static DifferentialDrive Create(RoboidControl.DifferentialDrive core) {
|
|
GameObject gameObj = new(core.name);
|
|
DifferentialDrive component = gameObj.AddComponent<DifferentialDrive>();
|
|
component.Init(core);
|
|
|
|
Rigidbody rb = gameObj.AddComponent<Rigidbody>();
|
|
rb.isKinematic = false;
|
|
rb.mass = 0.5f;
|
|
|
|
return component;
|
|
}
|
|
|
|
protected override void HandleBinary() {
|
|
RoboidControl.DifferentialDrive drive = core as RoboidControl.DifferentialDrive;
|
|
if (leftWheel == null) {
|
|
GameObject leftWheelObj = new GameObject("Left wheel");
|
|
leftWheelObj.transform.SetParent(this.transform);
|
|
leftWheel = leftWheelObj.AddComponent<WheelCollider>();
|
|
leftWheel.mass = 0.1f;
|
|
leftWheel.suspensionDistance = 0.01f;
|
|
leftWheel.suspensionSpring = new JointSpring {
|
|
spring = 1000f, // Very high spring value to make it rigid
|
|
damper = 100f, // Low damping (could be adjusted for slight 'bounciness')
|
|
targetPosition = 0.5f // Neutral position (middle of the suspension travel)
|
|
};
|
|
leftWheel.radius = drive.wheelRadius;
|
|
leftWheel.center = new Vector3(-drive.wheelSeparation / 2, 0, 0);
|
|
}
|
|
if (rightWheel == null) {
|
|
GameObject rightWheelObj = new GameObject("Left wheel");
|
|
rightWheelObj.transform.SetParent(this.transform);
|
|
rightWheel = rightWheelObj.AddComponent<WheelCollider>();
|
|
rightWheel.mass = 0.1f;
|
|
rightWheel.suspensionDistance = 0.01f;
|
|
rightWheel.suspensionSpring = new JointSpring {
|
|
spring = 1000f, // Very high spring value to make it rigid
|
|
damper = 100f, // Low damping (could be adjusted for slight 'bounciness')
|
|
targetPosition = 0.5f // Neutral position (middle of the suspension travel)
|
|
};
|
|
rightWheel.radius = drive.wheelRadius;
|
|
rightWheel.center = new Vector3(drive.wheelSeparation / 2, 0, 0);
|
|
|
|
}
|
|
// Thing[] children = this.GetComponentsInChildren<Thing>();
|
|
// if (leftWheel == null) {
|
|
// leftWheel = children.FirstOrDefault(child => child.core.id == drive.leftWheel.id);
|
|
// if (leftWheel == null) {
|
|
// RoboidControl.Thing coreThing = new(drive.owner, 0, drive.leftWheel.id, false) {
|
|
// name = "Left Wheel"
|
|
// };
|
|
// leftWheel = Thing.Create(coreThing);
|
|
// leftWheel.transform.SetParent(this.transform);
|
|
// }
|
|
// WheelCollider wheel = this.GetComponent<WheelCollider>();
|
|
// if (wheel == null)
|
|
// wheel = this.gameObject.AddComponent<WheelCollider>();
|
|
// wheel.mass = 0.1f;
|
|
// wheel.suspensionDistance = 0.01f;
|
|
// wheel.suspensionSpring = new JointSpring {
|
|
// spring = 1000f, // Very high spring value to make it rigid
|
|
// damper = 100f, // Low damping (could be adjusted for slight 'bounciness')
|
|
// targetPosition = 0.5f // Neutral position (middle of the suspension travel)
|
|
// };
|
|
// wheel.radius = drive.wheelRadius;
|
|
// wheel.center = new Vector3(-drive.wheelSeparation / 2, 0, 0);
|
|
// }
|
|
// if (rightWheel == null) {
|
|
// this.rightWheel = children.FirstOrDefault(child => child.core.id == drive.rightWheel.id);
|
|
// if (this.rightWheel == null) {
|
|
// RoboidControl.Thing coreThing = new(drive.owner, 0, drive.rightWheel.id, false) {
|
|
// name = "Right Wheel"
|
|
// };
|
|
// this.rightWheel = Thing.Create(coreThing);
|
|
// this.rightWheel.transform.SetParent(this.transform);
|
|
// }
|
|
// WheelCollider wheel = this.GetComponent<WheelCollider>();
|
|
// if (wheel == null)
|
|
// wheel = this.gameObject.AddComponent<WheelCollider>();
|
|
// wheel.mass = 0.1f;
|
|
// wheel.suspensionDistance = 0.01f;
|
|
// wheel.suspensionSpring = new JointSpring {
|
|
// spring = 1000f, // Very high spring value to make it rigid
|
|
// damper = 100f, // Low damping (could be adjusted for slight 'bounciness')
|
|
// targetPosition = 0.5f // Neutral position (middle of the suspension travel)
|
|
// };
|
|
// wheel.radius = drive.wheelRadius;
|
|
// wheel.center = new Vector3(drive.wheelSeparation / 2, 0, 0);
|
|
// }
|
|
}
|
|
}
|
|
|
|
}
|
|
#endif |