// ============================================================================================================= /// <summary> /// This function will be called when the ECS system decides to convert this GameObject to an equivalent ECS... /// ...Entity, allowing us to add the component this MonoBehaviour represents. /// </summary> /// <param name="entity"></param> /// <param name="dstManager"></param> /// <param name="conversionSystem"></param> public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { //First create the actual component and setup its data based on exposed properties. var rotationComponent = new RotationVelocity(); //Notice that this is not a reference, but a value that will be copied to the Entity Manager. rotationComponent.rotatingVelocity = rotationVelocity; //Tell the entity manager that this Component belongs to the entity we are converting. dstManager.AddComponentData(entity, rotationComponent); }
// ============================================================================================================= /// <summary> /// This function has the logic to apply to all entities with Rotation and RotationVelocity... /// ...components every frame /// </summary> /// <param name="entity">Entity that will come from our for each</param> /// <param name="rotation"></param> /// <param name="rotationVelocity"></param> private void RotateEntity(Entity entity, ref Rotation rotation, ref RotationVelocity rotationVelocity) { //We get the velocity specified in the component, apply delta time and convert degrees to radians. float rotationAmount = rotationVelocity.rotatingVelocity * Time.deltaTime * Mathf.Deg2Rad; //Create a quaternion that represents the desired amount of rotation. //Notice that this quaternion is not the same as the default Quaternion of MonoBehaviours. quaternion rotationQuat = quaternion.Euler(0, rotationAmount, 0); //Apply the desired rotation multiplying current rotation by the quaternion quaternion newRotation = math.mul(rotation.Value, rotationQuat); //Override the current rotation with the new rotated rotation; rotation.Value = newRotation; }