playMaker

Author Topic: Prime 31 Character Controller 2d Action  (Read 2137 times)

KellyRay

  • Full Member
  • ***
  • Posts: 169
    • View Profile
Prime 31 Character Controller 2d Action
« on: October 07, 2015, 11:38:55 AM »
Hey friends,

I translated the demo script into an input action script for Prime 31's open source 2d Character Controller:

https://github.com/prime31/CharacterController2D

Add the CharacterController2D Component to your player object and an FSM with this action on it. Be sure to configure the CharacterController2D with the appropriate tags and layers and what not.

The action uses the Horizontal/Vertical input axis. The original script controlled the animations as well, this script does not do this. Id suggest using an Axis Event to control animations.

Code: [Select]
using UnityEngine;
using System.Collections;
using Prime31;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Character)]
[Tooltip("Moves a Game Object with a Character Controller 2D from Prime31")]
public class P31CharacerterController2D : FsmStateAction

{
[Tooltip("The GameObject to move.")]
public FsmOwnerDefault gameObject;

[Tooltip("Gravity is applied automatically")]
public FsmFloat gravity;

[Tooltip("Horizontal speed.")]
public FsmFloat runSpeed;

[Tooltip("How fast the player can change directions. Higher equal faster")]
public FsmFloat groundDamping;

[Tooltip("How much control we have over the character while in the air")]
public FsmFloat inAirDamping;

[Tooltip("How high we can jump")]
public FsmFloat jumpHeight;

// movement config
//public float gravity = -25f;
//public float runSpeed = 8f;
//public float groundDamping = 20f; // how fast do we change direction? higher means faster
//public float inAirDamping = 5f;
//public float jumpHeight = 3f;

[HideInInspector]
private float normalizedHorizontalSpeed = 0;
private CharacterController2D _controller;
private RaycastHit2D _lastControllerColliderHit;
private Vector3 _velocity;

public override void Reset()
{
gravity = -25f;
runSpeed = 8f;
groundDamping = 20f;
inAirDamping = 5f;
jumpHeight = 3f;
}


public void OnStart()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);

_controller = go.GetComponent<CharacterController2D>();

// listen to some events for illustration purposes
_controller.onControllerCollidedEvent += onControllerCollider;
_controller.onTriggerEnterEvent += onTriggerEnterEvent;
_controller.onTriggerExitEvent += onTriggerExitEvent;
}


#region Event Listeners

void onControllerCollider( RaycastHit2D hit )
{
// bail out on plain old ground hits cause they arent very interesting
if( hit.normal.y == 1f )
return;

// logs any collider hits if uncommented. it gets noisy so it is commented out for the demo
//Debug.Log( "flags: " + _controller.collisionState + ", hit.normal: " + hit.normal );
}


void onTriggerEnterEvent( Collider2D col )
{
Debug.Log( "onTriggerEnterEvent: " + col.gameObject.name );
}


void onTriggerExitEvent( Collider2D col )
{
Debug.Log( "onTriggerExitEvent: " + col.gameObject.name );
}

#endregion


// the Update loop contains a very simple example of moving the character around and controlling the animation
public override void OnUpdate()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);

_controller = go.GetComponent<CharacterController2D>();

// listen to some events for illustration purposes
_controller.onControllerCollidedEvent += onControllerCollider;
_controller.onTriggerEnterEvent += onTriggerEnterEvent;
_controller.onTriggerExitEvent += onTriggerExitEvent;

if (_controller.isGrounded)
_velocity.y = 0;

if( Input.GetAxisRaw("Horizontal") > 0)
{
normalizedHorizontalSpeed = 1;
if( go.transform.localScale.x < 0f )
go.transform.localScale = new Vector3( -go.transform.localScale.x, go.transform.localScale.y, go.transform.localScale.z );

}
else if( Input.GetAxisRaw("Horizontal") < 0 )
{
normalizedHorizontalSpeed = -1;
if( go.transform.localScale.x > 0f )
go.transform.localScale = new Vector3( -go.transform.localScale.x, go.transform.localScale.y, go.transform.localScale.z );

}
else
{
normalizedHorizontalSpeed = 0;

}


// we can only jump whilst grounded
if( _controller.isGrounded && Input.GetAxisRaw("Vertical") > 0)
{
_velocity.y = Mathf.Sqrt( 2f * jumpHeight.Value * -gravity.Value );
}


// apply horizontal speed smoothing it. dont really do this with Lerp. Use SmoothDamp or something that provides more control
var smoothedMovementFactor = _controller.isGrounded ? groundDamping.Value : inAirDamping.Value; // how fast do we change direction?
_velocity.x = Mathf.Lerp( _velocity.x, normalizedHorizontalSpeed * runSpeed.Value, Time.deltaTime * smoothedMovementFactor );

// apply gravity before moving
_velocity.y += gravity.Value * Time.deltaTime;

// if holding down bump up our movement amount and turn off one way platform detection for a frame.
// this lets uf jump down through one way platforms
if( _controller.isGrounded && Input.GetAxisRaw("Vertical") < 0)
{
_velocity.y *= 3f;
_controller.ignoreOneWayPlatformsThisFrame = true;
}

_controller.move( _velocity * Time.deltaTime );

// grab our current _velocity to use as a base for all calculations
_velocity = _controller.velocity;
}
}
}

dudebxl

  • Hero Member
  • *****
  • Posts: 602
    • View Profile
Re: Prime 31 Character Controller 2d Action
« Reply #1 on: October 07, 2015, 01:10:25 PM »
Cool.. U should try and get into the ecosystem for the others.. :D

KellyRay

  • Full Member
  • ***
  • Posts: 169
    • View Profile
Re: Prime 31 Character Controller 2d Action
« Reply #2 on: October 08, 2015, 09:47:57 PM »
For sure! I'll see what I can do!

elvis75k

  • Junior Playmaker
  • **
  • Posts: 79
    • View Profile
    • nRender
Re: Prime 31 Character Controller 2d Action
« Reply #3 on: October 09, 2015, 11:55:27 PM »
You made my day, Thank You  :)

KellyRay

  • Full Member
  • ***
  • Posts: 169
    • View Profile
Re: Prime 31 Character Controller 2d Action
« Reply #4 on: October 21, 2015, 07:03:27 AM »
So, I'm stuck on how to get this action up to the eco system.

Not so much stuck as not sure how to follow the Github instructions as I'm super illiterate when it comes to repository systems and such.

Any help would be greatly appreciated!

dudebxl

  • Hero Member
  • *****
  • Posts: 602
    • View Profile
Re: Prime 31 Character Controller 2d Action
« Reply #5 on: November 26, 2015, 06:16:02 AM »
Hey Kelly,

Open a snipt account at https://snipt.net/ then copy paste the code. title should be the same as the title of code file - class name. Set the language - C# and add the tags (see my page for some examples: https://snipt.net/dudebxl/). Make public and save. Now in the ecosystem!  Super easy.

Hope it helps.

ps: I recently had problems saving the tags. if you cannot save does not matter, action will still be in ecosystem and then add the tags at a later date.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: Prime 31 Character Controller 2d Action
« Reply #6 on: November 27, 2015, 04:03:34 AM »
Hi,

 I made a video showing the whole process:


Bye,

 Jean