playMaker

Author Topic: Send Controller Collision Event  (Read 3946 times)

amaranth

  • Full Member
  • ***
  • Posts: 172
Send Controller Collision Event
« on: June 10, 2012, 12:36:13 AM »
This action sends an event when the character controller you specify does one of the following:

-hits an object with a specific tag
-is grounded
-hits something on the left or right side
-hits something above
-hits something below

Note: this action only works if the controller hits an object, not if an object hits the controller. If someone wants to enhance this script by adding this additional functionality, please do so, because I'm not sure how.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Character)]
[Tooltip("Sends an event when a collision occurs with a Character Controller on a Game Object.")]
public class SendControllerCollisionEvent : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(CharacterController))]
[Tooltip("The game object that contains the character controller.")]
public FsmOwnerDefault gameObject;

[UIHint(UIHint.Tag)]
[Tooltip("If you want to send an event when the character controller collides with an object that has a tag, select the tag here.")]
public FsmString tag;

[Tooltip("Sends an event when the character controller collides with an object with the tag specified above.")]
public FsmEvent tagEvent;

[Tooltip("Sends an event when the character controller is grounded.")]
public FsmEvent groundedEvent;

[Tooltip("Sends an event when the character controller collides with something on the left or right.")]
public FsmEvent sidesEvent;

[Tooltip("Sends an event when the character controller collides with something above it.")]
public FsmEvent aboveEvent;

[Tooltip("Sends an event when the character controller collides with something below it.")]
public FsmEvent belowEvent;

GameObject previousGo; // remember so we can get new controller only when it changes.
CharacterController controller; // the character controller

public override void Reset()
{
gameObject = null;
tag = "Untagged";
tagEvent = null;
groundedEvent = null;
sidesEvent = null;
aboveEvent = null;
belowEvent = null;
}

public override void OnUpdate()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;

if (go != previousGo)
{
controller = go.GetComponent<CharacterController>();
previousGo = go;
}

if (controller != null)
{
bool isGrounded = controller.isGrounded;
bool isSides = (controller.collisionFlags & CollisionFlags.Sides) != 0;
bool isAbove = (controller.collisionFlags & CollisionFlags.Above) != 0;
bool isBelow = (controller.collisionFlags & CollisionFlags.Below) != 0;

if (isGrounded == true)
Fsm.Event(groundedEvent);

if (isSides == true)
Fsm.Event(sidesEvent);

if (isAbove == true)
Fsm.Event(aboveEvent);

if (isBelow == true)
Fsm.Event(belowEvent);
}
}

public override void DoControllerColliderHit(ControllerColliderHit collisionInfo)
{
if (collisionInfo.collider.gameObject.tag == tag.Value)
Fsm.Event(tagEvent);
}

}
}

Dev_Sebas

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 398
    • Blog
Re: Send Controller Collision Event
« Reply #1 on: June 11, 2012, 04:37:17 PM »
Hi
Very nice script, nice work.
I canĀ“t script to but if some else could make the feature "hit something on front".HUAU that could be amazing!!! :-*
Bye