playMaker

Author Topic: Enable/Disable Character Controller  (Read 9363 times)

amaranth

  • Full Member
  • ***
  • Posts: 172
Enable/Disable Character Controller
« 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;
}
}

}
}
« Last Edit: May 15, 2012, 05:36:04 PM by amaranth »

Maru07

  • Playmaker Newbie
  • *
  • Posts: 2
Re: Enable/Disable Character Controller
« Reply #1 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

Nacho100

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Enable/Disable Character Controller
« Reply #2 on: July 26, 2020, 09:16:02 PM »
I know this is super old, but thank you man this was exactly what I needed  :)

Mark7of7

  • Playmaker Newbie
  • *
  • Posts: 1
Re: Enable/Disable Character Controller
« Reply #3 on: August 17, 2020, 10:36:46 AM »
Agreed. Old but perfect(like me). Just what I needed. Thanks

zerosimms

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Enable/Disable Character Controller
« Reply #4 on: June 04, 2021, 05:57:12 AM »
Thank you so much for sharing this, worked a treat!