This simple script is used to enable or disable the character controller attached to a game object. Have fun!
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Character)]
[Tooltip("Disables or enables a character controller.")]
public class EnableCharacterController : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(CharacterController))]
[Tooltip("The GameObject with the character controller.")]
public FsmOwnerDefault gameObject;
[Tooltip("Mark this to enable, unmark this to disable the character controller")]
public FsmBool enable;
[Tooltip("Mark this to return character controller to previous state when this action is over")]
public FsmBool resetOnExit;
private GameObject previousGo; // remember so we can get new controller only when it changes.
private CharacterController controller;
public override void Reset()
{
gameObject = null;
enable = true;
resetOnExit = true;
}
public override void OnEnter()
{
DoEnableCC();
Finish();
}
void DoEnableCC()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;
if (go != previousGo)
{
controller = go.GetComponent<CharacterController>();
previousGo = go;
}
if (controller != null)
{
controller.enabled = enable.Value;
}
}
public override void OnExit()
{
if (controller == null) return;
if (resetOnExit.Value)
{
controller.enabled = !enable.Value;
}
}
}
}