2024-08-16 10:24:40 +08:00
// Author: Daniele Giardini - http://www.demigiant.com
// Created: 2018/07/13
#if true // MODULE_MARKER
using DG.Tweening.Core ;
using DG.Tweening.Core.Enums ;
using DG.Tweening.Plugins ;
using DG.Tweening.Plugins.Core.PathCore ;
using DG.Tweening.Plugins.Options ;
using UnityEngine ;
#pragma warning disable 1591
namespace DG.Tweening
{
2024-12-04 23:15:05 +08:00
public static class DOTweenModulePhysics
2024-08-16 10:24:40 +08:00
{
#region Shortcuts
#region Rigidbody
/// <summary>Tweens a Rigidbody's position to the given value.
/// Also stores the rigidbody as the tween's target so it can be used for filtered operations</summary>
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
public static TweenerCore < Vector3 , Vector3 , VectorOptions > DOMove ( this Rigidbody target , Vector3 endValue , float duration , bool snapping = false )
{
TweenerCore < Vector3 , Vector3 , VectorOptions > t = DOTween . To ( ( ) = > target . position , target . MovePosition , endValue , duration ) ;
t . SetOptions ( snapping ) . SetTarget ( target ) ;
return t ;
}
/// <summary>Tweens a Rigidbody's X position to the given value.
/// Also stores the rigidbody as the tween's target so it can be used for filtered operations</summary>
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
public static TweenerCore < Vector3 , Vector3 , VectorOptions > DOMoveX ( this Rigidbody target , float endValue , float duration , bool snapping = false )
{
TweenerCore < Vector3 , Vector3 , VectorOptions > t = DOTween . To ( ( ) = > target . position , target . MovePosition , new Vector3 ( endValue , 0 , 0 ) , duration ) ;
t . SetOptions ( AxisConstraint . X , snapping ) . SetTarget ( target ) ;
return t ;
}
/// <summary>Tweens a Rigidbody's Y position to the given value.
/// Also stores the rigidbody as the tween's target so it can be used for filtered operations</summary>
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
public static TweenerCore < Vector3 , Vector3 , VectorOptions > DOMoveY ( this Rigidbody target , float endValue , float duration , bool snapping = false )
{
TweenerCore < Vector3 , Vector3 , VectorOptions > t = DOTween . To ( ( ) = > target . position , target . MovePosition , new Vector3 ( 0 , endValue , 0 ) , duration ) ;
t . SetOptions ( AxisConstraint . Y , snapping ) . SetTarget ( target ) ;
return t ;
}
/// <summary>Tweens a Rigidbody's Z position to the given value.
/// Also stores the rigidbody as the tween's target so it can be used for filtered operations</summary>
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
public static TweenerCore < Vector3 , Vector3 , VectorOptions > DOMoveZ ( this Rigidbody target , float endValue , float duration , bool snapping = false )
{
TweenerCore < Vector3 , Vector3 , VectorOptions > t = DOTween . To ( ( ) = > target . position , target . MovePosition , new Vector3 ( 0 , 0 , endValue ) , duration ) ;
t . SetOptions ( AxisConstraint . Z , snapping ) . SetTarget ( target ) ;
return t ;
}
/// <summary>Tweens a Rigidbody's rotation to the given value.
/// Also stores the rigidbody as the tween's target so it can be used for filtered operations</summary>
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
/// <param name="mode">Rotation mode</param>
public static TweenerCore < Quaternion , Vector3 , QuaternionOptions > DORotate ( this Rigidbody target , Vector3 endValue , float duration , RotateMode mode = RotateMode . Fast )
{
TweenerCore < Quaternion , Vector3 , QuaternionOptions > t = DOTween . To ( ( ) = > target . rotation , target . MoveRotation , endValue , duration ) ;
t . SetTarget ( target ) ;
t . plugOptions . rotateMode = mode ;
return t ;
}
/// <summary>Tweens a Rigidbody's rotation so that it will look towards the given position.
/// Also stores the rigidbody as the tween's target so it can be used for filtered operations</summary>
/// <param name="towards">The position to look at</param><param name="duration">The duration of the tween</param>
/// <param name="axisConstraint">Eventual axis constraint for the rotation</param>
/// <param name="up">The vector that defines in which direction up is (default: Vector3.up)</param>
public static TweenerCore < Quaternion , Vector3 , QuaternionOptions > DOLookAt ( this Rigidbody target , Vector3 towards , float duration , AxisConstraint axisConstraint = AxisConstraint . None , Vector3 ? up = null )
{
TweenerCore < Quaternion , Vector3 , QuaternionOptions > t = DOTween . To ( ( ) = > target . rotation , target . MoveRotation , towards , duration )
. SetTarget ( target ) . SetSpecialStartupMode ( SpecialStartupMode . SetLookAt ) ;
t . plugOptions . axisConstraint = axisConstraint ;
t . plugOptions . up = ( up = = null ) ? Vector3 . up : ( Vector3 ) up ;
return t ;
}
#region Special
/// <summary>Tweens a Rigidbody's position to the given value, while also applying a jump effect along the Y axis.
/// Returns a Sequence instead of a Tweener.
/// Also stores the Rigidbody as the tween's target so it can be used for filtered operations</summary>
/// <param name="endValue">The end value to reach</param>
/// <param name="jumpPower">Power of the jump (the max height of the jump is represented by this plus the final Y offset)</param>
/// <param name="numJumps">Total number of jumps</param>
/// <param name="duration">The duration of the tween</param>
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
public static Sequence DOJump ( this Rigidbody target , Vector3 endValue , float jumpPower , int numJumps , float duration , bool snapping = false )
{
if ( numJumps < 1 ) numJumps = 1 ;
float startPosY = 0 ;
float offsetY = - 1 ;
bool offsetYSet = false ;
Sequence s = DOTween . Sequence ( ) ;
Tween yTween = DOTween . To ( ( ) = > target . position , target . MovePosition , new Vector3 ( 0 , jumpPower , 0 ) , duration / ( numJumps * 2 ) )
. SetOptions ( AxisConstraint . Y , snapping ) . SetEase ( Ease . OutQuad ) . SetRelative ( )
. SetLoops ( numJumps * 2 , LoopType . Yoyo )
. OnStart ( ( ) = > startPosY = target . position . y ) ;
s . Append ( DOTween . To ( ( ) = > target . position , target . MovePosition , new Vector3 ( endValue . x , 0 , 0 ) , duration )
. SetOptions ( AxisConstraint . X , snapping ) . SetEase ( Ease . Linear )
) . Join ( DOTween . To ( ( ) = > target . position , target . MovePosition , new Vector3 ( 0 , 0 , endValue . z ) , duration )
. SetOptions ( AxisConstraint . Z , snapping ) . SetEase ( Ease . Linear )
) . Join ( yTween )
. SetTarget ( target ) . SetEase ( DOTween . defaultEaseType ) ;
2024-12-04 23:15:05 +08:00
yTween . OnUpdate ( ( ) = >
{
if ( ! offsetYSet )
{
2024-08-16 10:24:40 +08:00
offsetYSet = true ;
offsetY = s . isRelative ? endValue . y : endValue . y - startPosY ;
}
Vector3 pos = target . position ;
pos . y + = DOVirtual . EasedValue ( 0 , offsetY , yTween . ElapsedPercentage ( ) , Ease . OutQuad ) ;
target . MovePosition ( pos ) ;
} ) ;
return s ;
}
/// <summary>Tweens a Rigidbody's position through the given path waypoints, using the chosen path algorithm.
/// Also stores the Rigidbody as the tween's target so it can be used for filtered operations.
/// <para>NOTE: to tween a rigidbody correctly it should be set to kinematic at least while being tweened.</para>
/// <para>BEWARE: doesn't work on Windows Phone store (waiting for Unity to fix their own bug).
/// If you plan to publish there you should use a regular transform.DOPath.</para></summary>
/// <param name="path">The waypoints to go through</param>
/// <param name="duration">The duration of the tween</param>
/// <param name="pathType">The type of path: Linear (straight path), CatmullRom (curved CatmullRom path) or CubicBezier (curved with control points)</param>
/// <param name="pathMode">The path mode: 3D, side-scroller 2D, top-down 2D</param>
/// <param name="resolution">The resolution of the path (useless in case of Linear paths): higher resolutions make for more detailed curved paths but are more expensive.
/// Defaults to 10, but a value of 5 is usually enough if you don't have dramatic long curves between waypoints</param>
/// <param name="gizmoColor">The color of the path (shown when gizmos are active in the Play panel and the tween is running)</param>
public static TweenerCore < Vector3 , Path , PathOptions > DOPath (
this Rigidbody target , Vector3 [ ] path , float duration , PathType pathType = PathType . Linear ,
PathMode pathMode = PathMode . Full3D , int resolution = 10 , Color ? gizmoColor = null
)
{
if ( resolution < 1 ) resolution = 1 ;
TweenerCore < Vector3 , Path , PathOptions > t = DOTween . To ( PathPlugin . Get ( ) , ( ) = > target . position , target . MovePosition , new Path ( pathType , path , resolution , gizmoColor ) , duration )
. SetTarget ( target ) . SetUpdate ( UpdateType . Fixed ) ;
t . plugOptions . isRigidbody = true ;
t . plugOptions . mode = pathMode ;
return t ;
}
/// <summary>Tweens a Rigidbody's localPosition through the given path waypoints, using the chosen path algorithm.
/// Also stores the Rigidbody as the tween's target so it can be used for filtered operations
/// <para>NOTE: to tween a rigidbody correctly it should be set to kinematic at least while being tweened.</para>
/// <para>BEWARE: doesn't work on Windows Phone store (waiting for Unity to fix their own bug).
/// If you plan to publish there you should use a regular transform.DOLocalPath.</para></summary>
/// <param name="path">The waypoint to go through</param>
/// <param name="duration">The duration of the tween</param>
/// <param name="pathType">The type of path: Linear (straight path), CatmullRom (curved CatmullRom path) or CubicBezier (curved with control points)</param>
/// <param name="pathMode">The path mode: 3D, side-scroller 2D, top-down 2D</param>
/// <param name="resolution">The resolution of the path: higher resolutions make for more detailed curved paths but are more expensive.
/// Defaults to 10, but a value of 5 is usually enough if you don't have dramatic long curves between waypoints</param>
/// <param name="gizmoColor">The color of the path (shown when gizmos are active in the Play panel and the tween is running)</param>
public static TweenerCore < Vector3 , Path , PathOptions > DOLocalPath (
this Rigidbody target , Vector3 [ ] path , float duration , PathType pathType = PathType . Linear ,
PathMode pathMode = PathMode . Full3D , int resolution = 10 , Color ? gizmoColor = null
)
{
if ( resolution < 1 ) resolution = 1 ;
Transform trans = target . transform ;
TweenerCore < Vector3 , Path , PathOptions > t = DOTween . To ( PathPlugin . Get ( ) , ( ) = > trans . localPosition , x = > target . MovePosition ( trans . parent = = null ? x : trans . parent . TransformPoint ( x ) ) , new Path ( pathType , path , resolution , gizmoColor ) , duration )
. SetTarget ( target ) . SetUpdate ( UpdateType . Fixed ) ;
t . plugOptions . isRigidbody = true ;
t . plugOptions . mode = pathMode ;
t . plugOptions . useLocalPosition = true ;
return t ;
}
// Used by path editor when creating the actual tween, so it can pass a pre-compiled path
internal static TweenerCore < Vector3 , Path , PathOptions > DOPath (
this Rigidbody target , Path path , float duration , PathMode pathMode = PathMode . Full3D
)
{
TweenerCore < Vector3 , Path , PathOptions > t = DOTween . To ( PathPlugin . Get ( ) , ( ) = > target . position , target . MovePosition , path , duration )
. SetTarget ( target ) ;
t . plugOptions . isRigidbody = true ;
t . plugOptions . mode = pathMode ;
return t ;
}
internal static TweenerCore < Vector3 , Path , PathOptions > DOLocalPath (
this Rigidbody target , Path path , float duration , PathMode pathMode = PathMode . Full3D
)
{
Transform trans = target . transform ;
TweenerCore < Vector3 , Path , PathOptions > t = DOTween . To ( PathPlugin . Get ( ) , ( ) = > trans . localPosition , x = > target . MovePosition ( trans . parent = = null ? x : trans . parent . TransformPoint ( x ) ) , path , duration )
. SetTarget ( target ) ;
t . plugOptions . isRigidbody = true ;
t . plugOptions . mode = pathMode ;
t . plugOptions . useLocalPosition = true ;
return t ;
}
#endregion
#endregion
#endregion
2024-12-04 23:15:05 +08:00
}
2024-08-16 10:24:40 +08:00
}
#endif