您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
90 行
3.0 KiB
90 行
3.0 KiB
using NaughtyAttributes;
|
|
using UnityEngine;
|
|
|
|
namespace GameplayIngredients.Actions
|
|
{
|
|
public enum RigidbodyActionType { Force, Torque, ExplosionForce, Sleep };
|
|
public enum ActionSpace { Local, World };
|
|
|
|
[ExecuteAlways]
|
|
[Callable("Game", "Actions/ic-action-rigidbody.png")]
|
|
public class RigidbodyAction : ActionBase
|
|
{
|
|
public bool ApplyOnInstigator = false;
|
|
[HideIf("ApplyOnInstigator")]
|
|
public Rigidbody m_Rigidbody;
|
|
[OnValueChanged("OnParameterTypeChanged")]
|
|
public RigidbodyActionType actionType;
|
|
|
|
bool force = true;
|
|
bool explosion;
|
|
|
|
[ShowIf("force")]
|
|
public ActionSpace actionSpace;
|
|
[ShowIf("force")]
|
|
public ForceMode forceMode;
|
|
[ShowIf("force")]
|
|
public Vector3 direction;
|
|
[ShowIf("explosion")]
|
|
public float explosionForce;
|
|
[ShowIf("explosion")]
|
|
public Vector3 explositonPosition;
|
|
[ShowIf("explosion")]
|
|
public float explosionRadius;
|
|
|
|
public override void Execute(GameObject instigator = null)
|
|
{
|
|
Rigidbody target = m_Rigidbody;
|
|
|
|
if (ApplyOnInstigator)
|
|
target = instigator.GetComponent<Rigidbody>();
|
|
|
|
if (target == null)
|
|
{
|
|
Debug.LogWarning("Could not apply RigidbodyAction to null Rigidbody");
|
|
return;
|
|
}
|
|
|
|
switch (actionType)
|
|
{
|
|
case RigidbodyActionType.Force:
|
|
if(actionSpace == ActionSpace.World)
|
|
{
|
|
target.AddForce(direction, forceMode);
|
|
}
|
|
if(actionSpace == ActionSpace.Local)
|
|
{
|
|
target.AddRelativeForce(direction, forceMode);
|
|
}
|
|
break;
|
|
case RigidbodyActionType.Torque:
|
|
if (actionSpace == ActionSpace.World)
|
|
{
|
|
target.AddTorque(direction, forceMode);
|
|
}
|
|
if (actionSpace == ActionSpace.Local)
|
|
{
|
|
target.AddRelativeTorque(direction, forceMode);
|
|
}
|
|
break;
|
|
case RigidbodyActionType.ExplosionForce:
|
|
target.AddExplosionForce(explosionForce, explositonPosition, explosionRadius, 0, forceMode);
|
|
break;
|
|
case RigidbodyActionType.Sleep:
|
|
target.Sleep();
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void OnParameterTypeChanged()
|
|
{
|
|
force = (actionType == RigidbodyActionType.Force || actionType == RigidbodyActionType.Torque);
|
|
explosion = (actionType == RigidbodyActionType.ExplosionForce);
|
|
}
|
|
|
|
public override string GetDefaultName()
|
|
{
|
|
return $"RigidBody {actionType} on {(ApplyOnInstigator?"instigator":m_Rigidbody?.gameObject.name)}";
|
|
}
|
|
}
|
|
}
|