/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Character.Abilities
{
using UnityEngine;
///
/// Moves with the specified object. See this page for more info on the setup required:
/// https://opsive.com/support/documentation/ultimate-character-controller/character/abilities/included-abilities/move-with-object/
///
[DefaultStopType(AbilityStopType.Automatic)]
public class MoveWithObject : Ability
{
[Tooltip("The object that the character should move with.")]
[SerializeField] protected Transform m_Target;
public Transform Target { get { return m_Target; }
set {
var prevTarget = m_Target;
m_Target = value;
if (m_Target != null && m_Target.GetComponent() == null) {
Debug.Log("Error: The target " + Target.name + " does not have the Kinematic Object component. See the Move With Object documentation for more information.");
m_Target = null;
}
if (IsActive && prevTarget != null && m_Target != prevTarget) {
m_CharacterLocomotion.SetPlatform(m_Target);
}
}
}
public override bool IsConcurrent { get { return true; } }
///
/// Initailize the default values.
///
public override void Awake()
{
base.Awake();
// Set the property so it goes through the error check.
if (m_Target != null) {
Target = m_Target;
}
}
///
/// Can the ability be started?
///
/// True if the ability can be started.
public override bool CanStartAbility()
{
if (m_Target == null) {
return false;
}
return base.CanStartAbility();
}
///
/// The ability has started.
///
protected override void AbilityStarted()
{
base.AbilityStarted();
m_CharacterLocomotion.SetPlatform(m_Target);
}
///
/// Can the ability be stopped?
///
/// True if the ability can be stopped.
public override bool CanStopAbility()
{
if (m_Target != null) {
return false;
}
return base.CanStopAbility();
}
///
/// The ability has stopped running.
///
/// Was the ability force stopped?
protected override void AbilityStopped(bool force)
{
base.AbilityStopped(force);
m_CharacterLocomotion.SetPlatform(null, false);
}
}
}