using UnityEngine;
using FSA = UnityEngine.Serialization.FormerlySerializedAsAttribute;
namespace Lean.Common
{
/// This component allows you to add force to the current GameObject using events.
[HelpURL(LeanHelper.PlusHelpUrlPrefix + "LeanManualVelocity")]
[AddComponentMenu(LeanHelper.ComponentPathPrefix + "Manual Velocity")]
public class LeanManualVelocity : MonoBehaviour
{
/// If your Rigidbody is on a different GameObject, set it here.
public GameObject Target { set { target = value; } get { return target; } } [FSA("Target")] [SerializeField] private GameObject target;
/// The force mode.
public ForceMode Mode { set { mode = value; } get { return mode; } } [FSA("Mode")] [SerializeField] private ForceMode mode;
/// The applied velocity will be multiplied by this.
public float Multiplier { set { multiplier = value; } get { return multiplier; } } [FSA("Multiplier")] [SerializeField] private float multiplier = 1.0f;
/// The velocity space.
public Space Space { set { space = value; } get { return space; } } [FSA("Space")] [SerializeField] private Space space = Space.World;
/// The first force direction.
public Vector2 DirectionA { set { directionA = value; } get { return directionA; } } [FSA("DirectionA")] [SerializeField] private Vector2 directionA = Vector2.right;
/// The second force direction.
public Vector2 DirectionB { set { directionB = value; } get { return directionB; } } [FSA("DirectionB")] [SerializeField] private Vector2 directionB = Vector2.up;
public void AddForceA(float delta)
{
AddForce(directionA * delta);
}
public void AddForceB(float delta)
{
AddForce(directionB * delta);
}
public void AddForceAB(Vector2 delta)
{
AddForce(directionA * delta.x + directionB * delta.y);
}
public void AddForceFromTo(Vector3 from, Vector3 to)
{
AddForce(to - from);
}
public void AddForce(Vector3 delta)
{
var finalGameObject = target != null ? target : gameObject;
var rigidbody = finalGameObject.GetComponent();
if (rigidbody != null)
{
var force = delta * multiplier;
if (space == Space.Self)
{
force = rigidbody.transform.rotation * force;
}
rigidbody.AddForce(force, mode);
}
}
}
}
#if UNITY_EDITOR
namespace Lean.Common.Editor
{
using TARGET = LeanManualVelocity;
[UnityEditor.CanEditMultipleObjects]
[UnityEditor.CustomEditor(typeof(TARGET), true)]
public class LeanManualVelocity_Editor : LeanEditor
{
protected override void OnInspector()
{
TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts);
Draw("target", "If your Rigidbody is on a different GameObject, set it here.");
Draw("mode", "The force mode.");
Draw("multiplier", "The applied velocity will be multiplied by this.");
Draw("space", "The velocity space.");
Draw("directionA", "The first force direction.");
Draw("directionB", "The second force direction.");
}
}
}
#endif