Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

84 行
2.7 KiB

using NaughtyAttributes;
using UnityEngine;
namespace GameplayIngredients.Actions
{
public enum RigidbodyActionType { Force, Torque, ExplosionForce, Sleep };
public enum ActionSpace { Local, World };
[ExecuteAlways]
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);
}
}
}