76 lines
3.6 KiB
C#
76 lines
3.6 KiB
C#
#if UNITY_5_3_OR_NEWER
|
|
using UnityEngine;
|
|
|
|
namespace RoboidControl.Unity {
|
|
public class Wheel : Motor {
|
|
/// <summary>
|
|
/// Create the Unity representation
|
|
/// </summary>
|
|
/// <param name="core">The core motor</param>
|
|
/// <returns>The Unity representation of a motorised wheel</returns>
|
|
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<Wheel>();
|
|
if (component != null)
|
|
component.core = core;
|
|
return component;
|
|
}
|
|
else {
|
|
// Fallback implementation
|
|
GameObject gameObj = new(core.name);
|
|
Wheel component = gameObj.AddComponent<Wheel>();
|
|
component.Init(core);
|
|
// component.wheelCollider = gameObj.AddComponent<WheelCollider>();
|
|
// 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<Wheel>();
|
|
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<Wheel>();
|
|
SiteServer participant = FindAnyObjectByType<SiteServer>();
|
|
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<WheelCollider>();
|
|
// 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;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
#endif |