49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
#if UNITY_5_3_OR_NEWER
|
|
using UnityEngine;
|
|
|
|
namespace RoboidControl.Unity {
|
|
public class Motor : Thing {
|
|
/// <summary>
|
|
/// Create the Unity representation
|
|
/// </summary>
|
|
/// <param name="core">The core motor</param>
|
|
/// <returns>The Unity representation of a motor</returns>
|
|
public static Motor Create(RoboidControl.Motor core) {
|
|
GameObject prefab = (GameObject)Resources.Load("Motor");
|
|
if (prefab != null) {
|
|
// Use resource prefab when available
|
|
GameObject gameObj = Instantiate(prefab);
|
|
Motor component = gameObj.GetComponent<Motor>();
|
|
if (component != null)
|
|
component.core = core;
|
|
return component;
|
|
}
|
|
else {
|
|
// Fallback implementation
|
|
GameObject gameObj = new(core.name);
|
|
Motor component = gameObj.AddComponent<Motor>();
|
|
component.Init(core);
|
|
|
|
Rigidbody rb = gameObj.AddComponent<Rigidbody>();
|
|
rb.isKinematic = true;
|
|
return component;
|
|
}
|
|
}
|
|
|
|
public float rotationSpeed = 0.0f;
|
|
|
|
protected override void HandleBinary() {
|
|
RoboidControl.Motor coreMotor = core as RoboidControl.Motor;
|
|
this.rotationSpeed = coreMotor.targetSpeed;
|
|
}
|
|
|
|
protected override void Update() {
|
|
base.Update();
|
|
// We rotate the first child of the motor, which should be the axle.
|
|
if (this.transform.childCount > 0) {
|
|
this.transform.GetChild(0).Rotate(360 * this.rotationSpeed, 0, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif |