#if LITMOTION_SUPPORT_PHYSICS using UnityEngine; namespace LitMotion.Extensions { /// /// Provides binding extension methods for Rigidbody. /// public static class LitMotionRigidbodyExtensions { /// /// Create a motion data and bind it to Rigidbody.position /// /// The type of special parameters given to the motion data /// The type of adapter that support value animation /// This builder /// Target component /// Whether to use rigidbody.MovePosition() /// Handle of the created motion data. public static MotionHandle BindToPosition(this MotionBuilder builder, Rigidbody rigidbody, bool useMovePosition = true) where TOptions : unmanaged, IMotionOptions where TAdapter : unmanaged, IMotionAdapter { Error.IsNull(rigidbody); if (useMovePosition) { return builder.Bind(rigidbody, static (x, rigidbody) => { rigidbody.MovePosition(x); }); } else { return builder.Bind(rigidbody, static (x, rigidbody) => { rigidbody.position = x; }); } } /// /// Create a motion data and bind it to Rigidbody.position.x /// /// The type of special parameters given to the motion data /// The type of adapter that support value animation /// This builder /// Target component /// Whether to use rigidbody.MovePosition() /// Handle of the created motion data. public static MotionHandle BindToPositionX(this MotionBuilder builder, Rigidbody rigidbody, bool useMovePosition = true) where TOptions : unmanaged, IMotionOptions where TAdapter : unmanaged, IMotionAdapter { Error.IsNull(rigidbody); if (useMovePosition) { return builder.Bind(rigidbody, static (x, rigidbody) => { var p = rigidbody.position; p.x = x; rigidbody.MovePosition(p); }); } else { return builder.Bind(rigidbody, static (x, rigidbody) => { var p = rigidbody.position; p.x = x; rigidbody.position = p; }); } } /// /// Create a motion data and bind it to Rigidbody.position.y /// /// The type of special parameters given to the motion data /// The type of adapter that support value animation /// This builder /// Target component /// Whether to use rigidbody.MovePosition() /// Handle of the created motion data. public static MotionHandle BindToPositionY(this MotionBuilder builder, Rigidbody rigidbody, bool useMovePosition = true) where TOptions : unmanaged, IMotionOptions where TAdapter : unmanaged, IMotionAdapter { Error.IsNull(rigidbody); if (useMovePosition) { return builder.Bind(rigidbody, static (y, rigidbody) => { var p = rigidbody.position; p.y = y; rigidbody.MovePosition(p); }); } else { return builder.Bind(rigidbody, static (y, rigidbody) => { var p = rigidbody.position; p.y = y; rigidbody.position = p; }); } } /// /// Create a motion data and bind it to Rigidbody.position.z /// /// The type of special parameters given to the motion data /// The type of adapter that support value animation /// This builder /// Target component /// Whether to use rigidbody.MovePosition() /// Handle of the created motion data. public static MotionHandle BindToPositionZ(this MotionBuilder builder, Rigidbody rigidbody, bool useMovePosition = true) where TOptions : unmanaged, IMotionOptions where TAdapter : unmanaged, IMotionAdapter { Error.IsNull(rigidbody); if (useMovePosition) { return builder.Bind(rigidbody, static (z, rigidbody) => { var p = rigidbody.position; p.z = z; rigidbody.MovePosition(p); }); } else { return builder.Bind(rigidbody, static (z, rigidbody) => { var p = rigidbody.position; p.z = z; rigidbody.position = p; }); } } /// /// Create a motion data and bind it to Rigidbody.position.xy /// /// The type of special parameters given to the motion data /// The type of adapter that support value animation /// This builder /// Target component /// Whether to use rigidbody.MovePosition() /// Handle of the created motion data. public static MotionHandle BindToPositionXY(this MotionBuilder builder, Rigidbody rigidbody, bool useMovePosition = true) where TOptions : unmanaged, IMotionOptions where TAdapter : unmanaged, IMotionAdapter { Error.IsNull(rigidbody); if (useMovePosition) { return builder.Bind(rigidbody, static (x, rigidbody) => { var p = rigidbody.position; p.x = x.x; p.y = x.y; rigidbody.MovePosition(p); }); } else { return builder.Bind(rigidbody, static (x, rigidbody) => { var p = rigidbody.position; p.x = x.x; p.y = x.y; rigidbody.position = p; }); } } /// /// Create a motion data and bind it to Rigidbody.position.yz /// /// The type of special parameters given to the motion data /// The type of adapter that support value animation /// This builder /// Target component /// Whether to use rigidbody.MovePosition() /// Handle of the created motion data. public static MotionHandle BindToPositionYZ(this MotionBuilder builder, Rigidbody rigidbody, bool useMovePosition = true) where TOptions : unmanaged, IMotionOptions where TAdapter : unmanaged, IMotionAdapter { Error.IsNull(rigidbody); if (useMovePosition) { return builder.Bind(rigidbody, static (x, rigidbody) => { var p = rigidbody.position; p.y = x.x; p.z = x.y; rigidbody.MovePosition(p); }); } else { return builder.Bind(rigidbody, static (x, rigidbody) => { var p = rigidbody.position; p.y = x.x; p.z = x.y; rigidbody.position = p; }); } } /// /// Create a motion data and bind it to Rigidbody.position.xz /// /// The type of special parameters given to the motion data /// The type of adapter that support value animation /// This builder /// Target component /// Whether to use rigidbody.MovePosition() /// Handle of the created motion data. public static MotionHandle BindToPositionXZ(this MotionBuilder builder, Rigidbody rigidbody, bool useMovePosition = true) where TOptions : unmanaged, IMotionOptions where TAdapter : unmanaged, IMotionAdapter { Error.IsNull(rigidbody); if (useMovePosition) { return builder.Bind(rigidbody, static (x, rigidbody) => { var p = rigidbody.position; p.x = x.x; p.z = x.y; rigidbody.MovePosition(p); }); } else { return builder.Bind(rigidbody, static (x, rigidbody) => { var p = rigidbody.position; p.x = x.x; p.z = x.y; rigidbody.position = p; }); } } /// /// Create a motion data and bind it to Rigidbody.rotation /// /// The type of special parameters given to the motion data /// The type of adapter that support value animation /// This builder /// Target component /// Whether to use rigidbody.MoveRotation() /// Handle of the created motion data. public static MotionHandle BindToRotation(this MotionBuilder builder, Rigidbody rigidbody, bool useMoveRotation = true) where TOptions : unmanaged, IMotionOptions where TAdapter : unmanaged, IMotionAdapter { Error.IsNull(rigidbody); if (useMoveRotation) { return builder.Bind(rigidbody, static (x, rigidbody) => { rigidbody.MoveRotation(x); }); } else { return builder.Bind(rigidbody, static (x, rigidbody) => { rigidbody.rotation = x; }); } } } } #endif