Seminar_Cable_Joints_Unity/Assets/Extensions.cs

22 lines
712 B
C#
Raw Normal View History

2023-11-17 16:09:21 +01:00
using UnityEngine;
public static class Extensions
{
public static ComponentA GetOrAddComponent<ComponentA>(this GameObject gameObject)
where ComponentA : Component {
ComponentA result = gameObject.GetComponent<ComponentA>();
return result == null ? gameObject.AddComponent<ComponentA>() : result;
}
public static Vector2 Position2d(this Transform t) {
Vector3 pos = t.position;
return new Vector2(pos.x, pos.y);
}
2023-11-30 09:20:46 +01:00
public static float ActualDistanceInWorld(this DistanceJoint2D dist) {
2023-11-17 16:09:21 +01:00
return (dist.transform.TransformPoint(dist.anchor) -
2023-11-30 09:20:46 +01:00
dist.connectedBody.transform.TransformPoint(dist.connectedAnchor)).magnitude;
2023-11-17 16:09:21 +01:00
}
}