#if UNITY_5_3_OR_NEWER using UnityEngine; namespace RoboidControl.Unity { public class Wheel : Motor { /// /// Create the Unity representation /// /// The core motor /// The Unity representation of a motorised wheel public static Wheel Create(RoboidControl.Motor core, float wheelRadius) { GameObject prefab = (GameObject)Resources.Load("Wheel"); if (prefab != null) { // Use resource prefab when available GameObject gameObj = Instantiate(prefab); Wheel component = gameObj.GetComponent(); if (component != null) component.core = core; return component; } else { // Fallback implementation GameObject gameObj = new(core.name); Wheel component = gameObj.AddComponent(); component.Init(core); component.wheelCollider = gameObj.AddComponent(); component.wheelCollider.mass = 0.1f; component.wheelCollider.suspensionDistance = 0.01f; component.wheelCollider.suspensionSpring = new JointSpring { spring = 100f, // Very high spring value to make it rigid damper = 10f, // Low damping (could be adjusted for slight 'bounciness') targetPosition = 0.5f // Neutral position (middle of the suspension travel) }; Debug.Log("Create " + core.name); return component; } } public static Wheel Create(Rigidbody rb, byte thingId) { GameObject prefab = (GameObject)Resources.Load("Wheel"); if (prefab != null) { // Use resource prefab when available GameObject gameObj = Instantiate(prefab); Wheel component = gameObj.GetComponent(); if (component != null) component.core = new RoboidControl.Thing(RoboidControl.Thing.Type.UncontrolledMotor, false); return component; } else { // Fallback implementation GameObject gameObj = new("Wheel"); gameObj.transform.parent = rb.transform; Wheel component = gameObj.AddComponent(); SiteServer participant = FindAnyObjectByType(); RoboidControl.Thing core = participant.coreParticipant.Get(thingId); if (core == null) core = new(participant.coreParticipant, RoboidControl.Thing.Type.UncontrolledMotor, thingId, false); else { ; } component.Init(core); component.wheelCollider = gameObj.AddComponent(); component.wheelCollider.mass = 0.1f; component.wheelCollider.suspensionDistance = 0.01f; component.wheelCollider.suspensionSpring = new JointSpring { spring = 100f, // Very high spring value to make it rigid damper = 10f, // Low damping (could be adjusted for slight 'bounciness') targetPosition = 0.5f // Neutral position (middle of the suspension travel) }; Debug.Log("Create placeholder Wheel "); return component; } } public WheelCollider wheelCollider; protected override void HandlePose() { this.wheelCollider.center = core.position.ToVector3(); this.transform.position = Vector3.zero; // position is done with the center } protected override void Update() { UpdateThing(); if (wheelCollider.radius > 0) { float targetRotationSpeed = this.rotationSpeed * 2 * Mathf.PI; // 1 rotation per second in radians // Calculate the required motor torque float requiredTorque = (targetRotationSpeed * wheelCollider.mass) / wheelCollider.radius; // Set the motor torque wheelCollider.motorTorque = requiredTorque; } } } } #endif