UnityEngine method |
ECS equivalent |
DetachChildren
|
Use the following:
void DetachChildren(ref SystemState state, Entity e)
{
DynamicBuffer buffer = SystemAPI.GetBuffer(e);
state.EntityManager.RemoveComponent(buffer.AsNativeArray().Reinterpret(),
ComponentType.ReadWrite());
}
|
GetChild
|
Use the following:
Child child(ref SystemState state, Entity e, int index)
{
return SystemAPI.GetBuffer(e)[index];
}
|
GetLocalPositionAndRotation
|
Use the following:
void GetLocalPositionAndRotation(ref SystemState state, Entity e, out float3 localPosition, out quaternion localRotation)
{
LocalTransform transform = SystemAPI.GetComponent(e);
localPosition = transform.Position;
localRotation = transform.Rotation;
}
|
GetPositionAndRotation
|
Use the following:
void GetPositionAndRotation(ref SystemState state, Entity e, out float3 position, out quaternion rotation)
{
LocalToWorld l2w = SystemAPI.GetComponent(e);
position = l2w.Value.Translation();
rotation = l2w.Value.Rotation();
}
|
InverseTransformDirection
|
Use the following:
float3 InverseTransformDirection(ref SystemState state, Entity e, float3 direction)
{
LocalToWorld l2w = SystemAPI.GetComponent(e);
return math.inverse(l2w.Value).TransformDirection(direction);
}
|
InverseTransformPoint
|
Use the following:
float3 InverseTransformPoint(ref SystemState state, Entity e, float3 position)
{
LocalToWorld l2w = SystemAPI.GetComponent(e);
return math.inverse(l2w.Value).TransformPoint(worldPoint);
}
|
InverseTransformVector
|
Use the following:
float3 InverseTransformVector(ref SystemState state, Entity e, float3 vector)
{
return math.inverse(SystemAPI.GetComponent(e).Value)
.TransformDirection(vector);
}
|
IsChildOf
|
Use the following:
bool IsChildOf(ref SystemState state, Entity e, Entity parent)
{
return SystemAPI.HasComponent(e)
&& SystemAPI.GetComponent(e).Value == parent;
}
|
LookAt
|
Use the following:
void LookAt(ref SystemState state, Entity e, float3 target, float3 worldUp)
{
if (SystemAPI.HasComponent(e))
{
Entity parent = SystemAPI.GetComponent(e).Value;
float4x4 parentL2W = SystemAPI.GetComponent(parent).Value;
target = math.inverse(parentL2W).TransformPoint(target);
}
LocalTransform transform = SystemAPI.GetComponent(e);
quaternion rotation = quaternion.LookRotationSafe(target, worldUp);
SystemAPI.SetComponent(e, transform.WithRotation(rotation));
}
|
Rotate
|
In the Entities transform system, rotation is always expressed as a quaternion, and an angle is always in radians. There is functionality in the Mathematics package library to convert Euler angles into quaternions, and to convert degrees into radians.
With Space.Self , or if the entity has no parent:
public void Rotate(ref SystemState state, Entity e, quaternion rotation)
{
LocalTransform transform = SystemAPI.GetComponent(e);
rotation = math.mul(rotation, transform.Rotation);
SystemAPI.SetComponent(e, transform.WithRotation(rotation));
}
With Space.World , and the entity may have a parent:
void Rotate(ref SystemState state, Entity e, quaternion rotation)
{
if (SystemAPI.HasComponent(e))
{
Entity parent = SystemAPI.GetComponent(e).Value;
float4x4 parentL2W = SystemAPI.GetComponent(parent).Value;
rotation = math.inverse(parentL2W).TransformRotation(rotation);
}
LocalTransform transform = SystemAPI.GetComponent(e);
rotation = math.mul(rotation, transform.Rotation);
SystemAPI.SetComponent(e, transform.WithRotation(rotation));
}
|
RotateAround
|
Use the following:
public void RotateAround(ref SystemState state, Entity e, float3 point, float3 axis, float angle)
{
if (SystemAPI.HasComponent(e))
{
Entity parent = SystemAPI.GetComponent(e).Value;
float4x4 parentL2W = SystemAPI.GetComponent(parent).Value;
float4x4 invParentL2W = math.inverse(parentL2W);
point = invParentL2W.TransformPoint(point);
axis = invParentL2W.TransformDirection(axis);
}
var transform = SystemAPI.GetComponent(e);
var q = quaternion.AxisAngle(axis, angle);
transform.Position = point + math.mul(q, transform.Position - point);
transform.Rotation = math.mul(q, transform.Rotation);
SystemAPI.SetComponent(e, transform);
}
|
SetLocalPositionAndRotation
|
Use the following:
void SetLocalPositionAndRotation(ref SystemState state, Entity e, float3 localPosition, quaternion localRotation)
{
SystemAPI.SetComponent(e, LocalTransform.FromPositionRotation(localPosition, localRotation));
}
|
SetParent
|
Without worldPositionStays :
void SetParent(ref SystemState state, Entity e, Entity parent)
{
SystemAPI.SetComponent(e, new Parent { Value = parent});
}
With worldPositionStays :
void SetParent(ref SystemState state, Entity e, Entity parent)
{
float4x4 childL2W = SystemAPI.GetComponent(e).Value;
float4x4 parentL2W = SystemAPI.GetComponent(parent).Value;
float4x4 temp = math.mul(math.inverse(parentL2W), childL2W);
SystemAPI.SetComponent(e, new Parent { Value = parent});
SystemAPI.SetComponent(e, LocalTransform.FromMatrix(temp));
}
|
SetPositionAndRotation
|
If the entity has no parent:
void SetPositionAndRotationA(ref SystemState state, Entity e, float3 position, quaternion rotation)
{
SystemAPI.SetComponent(e, LocalTransform.FromPositionRotation(position, rotation));
}
If the entity has a parent:
void SetPositionAndRotationB(ref SystemState state, Entity e, float3 position, quaternion rotation)
{
if (SystemAPI.HasComponent(e))
{
Entity parent = SystemAPI.GetComponent(e).Value;
float4x4 parentL2W = SystemAPI.GetComponent(parent).Value;
float4x4 invParentL2W = math.inverse(parentL2W);
position = invParentL2W.TransformPoint(position);
rotation = invParentL2W.TransformRotation(rotation);
}
SystemAPI.SetComponent(e, LocalTransform.FromPositionRotation(position, rotation));
}
|
TransformDirection
|
Use the following:
float3 TransformDirection(ref SystemState state, Entity e, float3 direction)
{
float3 temp = SystemAPI.GetComponent(e).Value.TransformDirection(direction);
return temp * (math.length(direction) / math.length(temp));
}
|
TransformPoint
|
Use the following:
float3 TransformPoint(ref SystemState state, Entity e, float3 position)
{
return SystemAPI.GetComponent(e).Value.TransformPoint(position);
}
|
Transformvector
|
Use the following:
float3 TransformVector(ref SystemState state, Entity e, float3 vector)
{
return SystemAPI.GetComponent(e).Value.TransformDirection(vector);
}
|
Translate
|
With Space.Self , or if the entity has no parent:
void Translate(ref SystemState state, Entity e, float3 translation)
{
SystemAPI.GetComponentRW(e, false).ValueRW.Position += translation;
}
With Space.World , and the entity may have a parent:
void Translate(ref SystemState state, Entity e, float3 translation)
{
if (SystemAPI.HasComponent(e))
{
Entity parent = SystemAPI.GetComponent(e).Value;
float4x4 parentL2W = SystemAPI.GetComponent(parent).Value;
translation = math.inverse(parentL2W).TransformDirection(translation);
}
SystemAPI.GetComponentRW(e, false).ValueRW.Position += translation;
}
|