Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: amaranth on May 11, 2012, 05:27:04 PM

Title: Enable/Disable Character Controller
Post by: amaranth on May 11, 2012, 05:27:04 PM
This simple script is used to enable or disable the character controller attached to a game object. Have fun!

Code: [Select]
// (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;
}
}

}
}
Title: Re: Enable/Disable Character Controller
Post by: Maru07 on August 06, 2012, 10:44:49 AM
Wow thx this is extremely Helpful :D
Could u write one for activating a collider as well? :D
Title: Re: Enable/Disable Character Controller
Post by: Nacho100 on July 26, 2020, 09:16:02 PM
I know this is super old, but thank you man this was exactly what I needed  :)
Title: Re: Enable/Disable Character Controller
Post by: Mark7of7 on August 17, 2020, 10:36:46 AM
Agreed. Old but perfect(like me). Just what I needed. Thanks
Title: Re: Enable/Disable Character Controller
Post by: zerosimms on June 04, 2021, 05:57:12 AM
Thank you so much for sharing this, worked a treat!