97 lines
4.5 KiB
C#
97 lines
4.5 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(Transform parent, 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 = parent;
|
|
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);
|
|
SphereCollider collider = gameObj.AddComponent<SphereCollider>();
|
|
collider.radius = 0.00001f;
|
|
collider.material = slidingWheel;
|
|
// 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;
|
|
}
|
|
}
|
|
|
|
private static PhysicMaterial _slidingWheel;
|
|
public static PhysicMaterial slidingWheel {
|
|
get {
|
|
if (_slidingWheel == null) {
|
|
_slidingWheel = new() {
|
|
dynamicFriction = 0.02f, // Dynamic friction
|
|
staticFriction = 0.01f, // Static friction
|
|
bounciness = 0f, // Bounciness (bounciness value between 0 and 1)
|
|
|
|
// Set the friction combine and bounce combine methods
|
|
frictionCombine = PhysicMaterialCombine.Minimum, // How to combine friction
|
|
bounceCombine = PhysicMaterialCombine.Average // How to combine bounciness
|
|
};
|
|
}
|
|
return _slidingWheel;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
#endif |