playMaker

Author Topic: Creating a deadzone in mobile accelerometer controls?[SOLVED]  (Read 5365 times)

derkoi

  • Full Member
  • ***
  • Posts: 187
Creating a deadzone in mobile accelerometer controls?[SOLVED]
« on: September 19, 2012, 07:27:33 AM »
I'm trying to create a dead zone from the accelerometer input. I've clamped the float to -1 and 1, I'd like to have an adjustable deadzone at 0, but I'm not sure how to tackle it.

Any suggestions?? Thanks
« Last Edit: September 24, 2012, 03:07:22 AM by jeanfabre »

Sjones

  • Full Member
  • ***
  • Posts: 203
Re: Creating a deadzone in mobile accelerometer controls?
« Reply #1 on: September 19, 2012, 07:24:10 PM »
just made this, its not tested but should work
Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Logic)]
[Tooltip("Clamps between high and low and a deadzone center.")]
public class FloatclampAndDeadZone : FsmStateAction
{
public FsmFloat accelerometerInput;
public FsmFloat minClamp;
public FsmFloat maxClamp;
public FsmFloat minDeadZone;
public FsmFloat maxDeadZone;
public FsmFloat clampOutput;
public bool everyFrame;

public override void Reset()
{
accelerometerInput = 0f;
minClamp = 0f;
maxClamp = 0f;
minDeadZone = 0f;
maxDeadZone = 0f;
clampOutput = 0f;
everyFrame = false;
}

public override void OnEnter()
{
DoCompare();

if (!everyFrame)
Finish();
}

public override void OnUpdate()
{
DoCompare();
}

void DoCompare()
{
if (accelerometerInput.Value > minDeadZone.Value &&
accelerometerInput.Value < maxDeadZone.Value)
{
clampOutput.Value = 0;
}
if (accelerometerInput.Value > maxClamp.Value)
{
clampOutput.Value = maxClamp.Value;
}
if (accelerometerInput.Value < minClamp.Value)
{
clampOutput.Value = minClamp.Value;
}
}
}
}

if there is no issue please let me know and can add it to the custom action forum

derkoi

  • Full Member
  • ***
  • Posts: 187
Re: Creating a deadzone in mobile accelerometer controls?
« Reply #2 on: September 20, 2012, 04:17:51 AM »
Thanks, it works but doesn't seem to have the desired effect. Might be me though.  :)

Sjones

  • Full Member
  • ***
  • Posts: 203
Re: Creating a deadzone in mobile accelerometer controls?
« Reply #3 on: September 20, 2012, 05:41:15 AM »
cool, did you want it to ease in and out of the deadzone? it currently leaves the value as it is, if its within the dead zone it just sets it to 0, I can offset the value  by the deadzone, so that as soon as you come out of the deadzone it starts counting up giving a smoother transition between deadzone and not

derkoi

  • Full Member
  • ***
  • Posts: 187
Re: Creating a deadzone in mobile accelerometer controls?
« Reply #4 on: September 20, 2012, 05:42:26 AM »
Yeah that sounds better suited, thanks.  :)

Sjones

  • Full Member
  • ***
  • Posts: 203
Re: Creating a deadzone in mobile accelerometer controls?
« Reply #5 on: September 20, 2012, 07:35:34 AM »
Here try this, again untested but should work

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Logic)]
[Tooltip("Clamps between high and low and a deadzone center, no jump in or out of deadzone.")]
public class FloatclampAndDeadZone : FsmStateAction
{
public FsmFloat accelerometerInput;
public FsmFloat minClamp;
public FsmFloat maxClamp;
public FsmFloat minDeadZone;
public FsmFloat maxDeadZone;
public FsmFloat clampOutput;
public bool everyFrame;

public override void Reset()
{
accelerometerInput = 0f;
minClamp = 0f;
maxClamp = 0f;
minDeadZone = 0f;
maxDeadZone = 0f;
clampOutput = 0f;
everyFrame = false;
}

public override void OnEnter()
{
DoCompare();

if (!everyFrame)
Finish();
}

public override void OnUpdate()
{
DoCompare();
}

void DoCompare()
{
if (accelerometerInput.Value > minDeadZone.Value &&
accelerometerInput.Value < maxDeadZone.Value)
{
clampOutput.Value = 0;
}
if (accelerometerInput.Value > maxClamp.Value)
{
clampOutput.Value = maxClamp.Value;
}
if (accelerometerInput.Value < minClamp.Value)
{
clampOutput.Value = minClamp.Value;
}
if (accelerometerInput.Value > maxDeadZone.Value &&
accelerometerInput.Value < maxClamp.Value)
{
clampOutput.Value = accelerometerInput.Value - maxDeadZone.Value;
}
if (accelerometerInput.Value < minDeadZone.Value &&
accelerometerInput.Value > minClamp.Value)
{
clampOutput.Value = accelerometerInput.Value + minDeadZone.Value;
}
}
}
}

if there is a problem its down to my math, in which case I just need to turn the equation around XP

derkoi

  • Full Member
  • ***
  • Posts: 187
Re: Creating a deadzone in mobile accelerometer controls?
« Reply #6 on: September 23, 2012, 05:41:46 AM »
Thanks, the 2nd one works great!  ;D