playMaker

Author Topic: Axis Event extended: 8 ways direction support  (Read 5581 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14595
  • Official Playmaker Support
    • View Profile
Axis Event extended: 8 ways direction support
« on: July 28, 2013, 06:57:20 AM »
Hi Everyone,

Following a request, please find a variant of AxisEvent action that has the following improvments:

-- handles 8 ways:
-- ability to get the signed angle of the axis direction, ranging from -180 to 180, 0 being fully vertical.
-- ability to send discrete events, only when the direction changed. This is imo the best way to go and it avoids infinite loops. so it's turned on by default. I have attached a working scene as well so that you can see how it works.

Bye,

 Jean
« Last Edit: July 28, 2013, 07:00:05 AM by jeanfabre »

jorren

  • Playmaker Newbie
  • *
  • Posts: 2
    • View Profile
Re: Axis Event extended: 8 ways direction support
« Reply #1 on: August 04, 2013, 08:06:07 AM »
Hi Jean,

thanks for the script, looks very useful.

Could you please explain the use of a discrete event? I'm not exactly sure what you mean.

Cheers,
J

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14595
  • Official Playmaker Support
    • View Profile
Re: Axis Event extended: 8 ways direction support
« Reply #2 on: August 05, 2013, 12:40:31 AM »
Hi,

 If you don't use "discrete event", it will fire EVERY frame what is the current direction. If you use "discrete event", it will only fire the event when the direction has changed.

Does that help?

bye,

 Jean

jorren

  • Playmaker Newbie
  • *
  • Posts: 2
    • View Profile
Re: Axis Event extended: 8 ways direction support
« Reply #3 on: August 05, 2013, 12:47:43 AM »
It does indeed. Thanks!

SeanC

  • Junior Playmaker
  • **
  • Posts: 60
    • View Profile
Re: Axis Event extended: 8 ways direction support
« Reply #4 on: October 09, 2013, 10:57:34 PM »
Hi all,

I'm trying to modify this action to give me 16 degrees of direction rather than eight, so there would be events fired every 22.5 degrees, rather than every 45. So, for example, there would be North (N), North by Northwest (NNW), Northwest (NW), etc.

I managed to get the new action to function and actually fire events, but they dont fire when I would expect. I am most definitely NOT a programmer, and would love it if someone would be kind enough to take the time to help me out.



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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Input)]
[Tooltip("Sends events based on the direction of Input Axis (Left/Right/Up/Down...). Also sends intermedidate directions ( left-Right). Expose an option to only send event when direction changed.")]
public class AxisEventExtended2 : FsmStateAction
{
[Tooltip("Horizontal axis as defined in the Input Manager")]
public FsmString horizontalAxis;

[Tooltip("Vertical axis as defined in the Input Manager")]
public FsmString verticalAxis;

[UIHint(UIHint.Variable)]
[Tooltip("The direction angle. Range from -180 to 180, 0 being full up")]
public FsmFloat storeAngle;

[Tooltip("Event to send if input is to the left.")]
public FsmEvent leftEvent;

[Tooltip("Event to send if input is to the right.")]
public FsmEvent rightEvent;

[Tooltip("Event to send if input is to the up.")]
public FsmEvent upEvent;

[Tooltip("Event to send if input is to the down.")]
public FsmEvent downEvent;

[Tooltip("Event to send if input is to the up left.")]
public FsmEvent upLeftEvent;

[Tooltip("Event to send if input is to the up right.")]
public FsmEvent upRightEvent;

[Tooltip("Event to send if input is to the down left.")]
public FsmEvent downLeftEvent;

[Tooltip("Event to send if input is to the down right.")]
public FsmEvent downRightEvent;

[Tooltip("Event to send if input is to the up right.")]
public FsmEvent NNEEvent;

[Tooltip("Event to send if input is to the down left.")]
public FsmEvent NNWEvent;

[Tooltip("Event to send if input is to the down right.")]
public FsmEvent SSEEvent;

[Tooltip("Event to send if input is to the up right.")]
public FsmEvent SSWEvent;

[Tooltip("Event to send if input is to the down left.")]
public FsmEvent WNWEvent;

[Tooltip("Event to send if input is to the down right.")]
public FsmEvent WSWEvent;

[Tooltip("Event to send if input is to the down left.")]
public FsmEvent ENEEvent;

[Tooltip("Event to send if input is to the down right.")]
public FsmEvent ESEEvent;

[Tooltip("Event to send if no axis input (centered).")]
public FsmEvent noDirection;







[Tooltip("Only send events when direction changes")]
public bool discreteEvents;

private int currentDirection =-2;

public override void Reset()
{
horizontalAxis = "Horizontal";
verticalAxis = "Vertical";
leftEvent = null;
rightEvent = null;
upEvent = null;
downEvent = null;
upLeftEvent = null;
upRightEvent = null;
downLeftEvent = null;
downRightEvent = null;
NNEEvent = null;
NNWEvent = null;
SSEEvent = null;
SSWEvent = null;
WNWEvent = null;
WSWEvent = null;
ENEEvent = null;
ESEEvent = null;
noDirection = null;

discreteEvents = true;


}

public override void OnEnter()
{
currentDirection = GetCurrentDirection();
}

public override void OnUpdate()
{
int direction = GetCurrentDirection();

if (currentDirection==direction && discreteEvents)
{
return;
}


if (direction<0 && noDirection != null)
{
Fsm.Event(noDirection);
}
// send events bases on direction

if (direction == 0 && rightEvent != null)
{
Fsm.Event(rightEvent);
//Debug.Log("Right");
}
else if (direction == 1 && upRightEvent != null)
{
Fsm.Event(upRightEvent);
//Debug.Log("UpRight");
}
else if (direction == 2 && upEvent != null)
{
Fsm.Event(upEvent);
//Debug.Log("Up");
}
else if (direction == 3 && upLeftEvent != null)
{
Fsm.Event(upLeftEvent);
//Debug.Log("upLeftEvent");
}
else if (direction == 4 && leftEvent != null)
{
Fsm.Event(leftEvent);
//Debug.Log("LeftEvent");
}
else if (direction == 5 && downLeftEvent != null)
{
Fsm.Event(downLeftEvent);
//Debug.Log("downLeftEvent");
}
else if (direction == 6 && downEvent != null)
{
Fsm.Event(downEvent);
//Debug.Log("downEvent");
}
else if (direction == 7 && downRightEvent != null)
{
Fsm.Event(downRightEvent);
//Debug.Log("downRightEvent");
}
else if (direction == 8 && NNWEvent != null)
{
Fsm.Event(NNWEvent);
//Debug.Log("downRightEvent");
}
else if (direction == 9 && NNEEvent != null)
{
Fsm.Event(NNEEvent);
//Debug.Log("downRightEvent");
}
else if (direction == 10 && SSEEvent != null)
{
Fsm.Event(SSEEvent);
//Debug.Log("downRightEvent");
}
else if (direction == 11 && SSWEvent != null)
{
Fsm.Event(SSWEvent);
//Debug.Log("downRightEvent");
}
else if (direction == 12 && WNWEvent != null)
{
Fsm.Event(WNWEvent);
//Debug.Log("downRightEvent");
}
else if (direction == 13 && WSWEvent != null)
{
Fsm.Event(WSWEvent);
//Debug.Log("downRightEvent");
}
else if (direction == 14 && ENEEvent != null)
{
Fsm.Event(ENEEvent);
//Debug.Log("downRightEvent");
}
else if (direction == 15 && ESEEvent != null)
{
Fsm.Event(ESEEvent);
//Debug.Log("downRightEvent");
}
}

private int GetCurrentDirection()
{
var x = horizontalAxis.Value != "" ? Input.GetAxis(horizontalAxis.Value) : 0;
var y = verticalAxis.Value != "" ? Input.GetAxis(verticalAxis.Value) : 0;

// get squared offset from center

var offset = (x * x) + (y * y);

// no offset?

if (offset.Equals(0))
{
if (!storeAngle.IsNone)
{
storeAngle.Value = 0;
}
return -1;
}

float rawAngle = (Mathf.Atan2(y, x) * Mathf.Rad2Deg);
if (! storeAngle.IsNone)
{
storeAngle.Value = rawAngle;
}

var angle = rawAngle + 22.5f;
if (angle < 0f)
{
angle += 360f;
}

return (int)(angle / 22.5);
}
}
}


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14595
  • Official Playmaker Support
    • View Profile
Re: Axis Event extended: 8 ways direction support
« Reply #5 on: October 10, 2013, 02:02:31 AM »
Hi,

 Raw Angle needs to be add 11.25, not 22.5

 I have attached a custom action for this.

Bye,

 Jean

SeanC

  • Junior Playmaker
  • **
  • Posts: 60
    • View Profile
Re: Axis Event extended: 8 ways direction support
« Reply #6 on: October 10, 2013, 07:37:40 AM »
Wierd, I tried that and got funky results, so I left it at 22.5, but i must have messed something up. Well thank you Jean. I will test this out later today!

GaTechGrad

  • Playmaker Newbie
  • *
  • Posts: 4
    • View Profile
Re: Axis Event extended: 8 ways direction support
« Reply #7 on: June 09, 2016, 12:04:35 AM »
The Axis Event Extended action with the Discrete Event option enabled was exactly what I needed to handle gamepad input for my latest puzzle game.  The only problem I had was that if I pressed in one direction multiple times, then the event would only fire once.
I looked at the source code for the action, and I noticed that the currentDirection value is only set when OnEnter is called.  Since the state only changed in my FSM when the joystick axis was moved in a direction, the OnEnter method was not getting called again when the same direction was pressed multiple times in the same direction.  This is because the currentDirection variable in the action did not change after the joystick moved back to the neutral position.  With Discrete Events enabled, the direction event only gets fired if the currentDirection variable is different from the direction being pressed.
To fix this, I created an event (I called it doNoDirection) which I set to the No Direction dropdown for the action, and I made a transition that loops back to its own state.
Now, for instance, if I press right twice on the game pad, the right event fires, then the no direction event fires (looping back to the state which invokes OnEnter, resetting the currentDirection variable), then the right direction fires again (since the current direction is set to no direction, which is different from right).
I hope that makes sense, and I just wanted to share in case anyone else was having the same problem.

Airblaster

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Re: Axis Event extended: 8 ways direction support
« Reply #8 on: July 08, 2016, 01:29:28 AM »
Does the package still work, my scene is empty?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14595
  • Official Playmaker Support
    • View Profile
Re: Axis Event extended: 8 ways direction support
« Reply #9 on: July 22, 2016, 12:49:46 AM »
Hi,

 The package only provides new custom actions, no scenes, you'll find them in the actions browser.

 Bye,

 Jean