playMaker

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

amaranth

  • Full Member
  • ***
  • Posts: 172
    • View Profile
Enable/Disable Character Controller
« on: May 11, 2012, 02: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, 02:36:04 PM by amaranth »

Maru07

  • Playmaker Newbie
  • *
  • Posts: 2
    • View Profile
Re: Enable/Disable Character Controller
« Reply #1 on: August 06, 2012, 07: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: 1
    • View Profile
Re: Enable/Disable Character Controller
« Reply #2 on: July 26, 2020, 06:16:02 PM »
I know this is super old, but thank you man this was exactly what I needed  :)

Mark7of7

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