playMaker

Author Topic: Get Signed Angle To Target, but with X axis  (Read 7689 times)

Kubold

  • Full Member
  • ***
  • Posts: 112
Get Signed Angle To Target, but with X axis
« on: January 21, 2014, 04:27:35 PM »
Hey!

I love Get Signed Angle To Target (Get Euler Delta Angle, http://hutonggames.com/playmakerforum/index.php?topic=3027.msg13819#msg13819) action. I use it all the time. Problem is that it only counts angles along Y axis.

The problem is that I want now to get signed angle along X axis (so I can do additive animations for aiming not only horizontally, but also vertically).
I think it should be a minor change in the code, but I can't get it right... I just don't know code.

I messed around with this, it gets SOME angle, but it flips and is not accurate...
Can somebody just correct it or make a new action (preferably, you should be able to choose an axis for counting the angle).

Code: [Select]
// (c) copyright Hutong Games, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Get the signed angle around the y axis between the gameObject and a target gameObject. Allow you to retrieve both positive and negative angle. Usefull for example if you want to get the delta angle always clockwise ( positive)")]
public class GetEulerDeltaAngleX : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;

public FsmGameObject target;

[UIHint(UIHint.Variable)]
[Tooltip("The shortest Angle. unsigned.")]
public FsmFloat angle;

[UIHint(UIHint.Variable)]
[Tooltip("The signed Angle. This is also the shortest distance.")]
public FsmFloat signedAngle;

[UIHint(UIHint.Variable)]
[Tooltip("The positive delta angle. This means the ClockWise travel to reach the target")]
public FsmFloat resultPositiveAngle;

[UIHint(UIHint.Variable)]
[Tooltip("The negative delta angle. This means the Counter ClockWise travel to reach the target")]
public FsmFloat resultNegativeAngle;


[Tooltip("Repeat this action every frame.")]
public bool everyFrame;


public override void Reset()
{
gameObject = null;
target = null;
signedAngle = null;
resultPositiveAngle = null;
resultNegativeAngle = null;
}

public override void OnEnter()
{
DoGetSignedAngle();

if (!everyFrame)
{
Finish();
}
}

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


void DoGetSignedAngle()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);

if (go == null)
{
return;
}

GameObject goTarget = target.Value;
if (goTarget == null)
{
return;
}

angle.Value = Quaternion.Angle(go.transform.rotation, goTarget.transform.rotation);

// get a "forward vector" for each rotation
Vector3 forwardA = go.transform.rotation * Vector3.up;
Vector3 forwardB = goTarget.transform.rotation * Vector3.up;

// get a numeric angle for each vector, on the X-Z plane (relative to world forward)
float angleA = Mathf.Atan2(forwardA.y, forwardA.z) * Mathf.Rad2Deg;
float angleB = Mathf.Atan2(forwardB.y, forwardB.z) * Mathf.Rad2Deg;

// get the signed difference in these angles
float _signedAngle = Mathf.DeltaAngle( angleA, angleB );

signedAngle.Value = _signedAngle;
if (_signedAngle <0){
resultNegativeAngle.Value = _signedAngle;
resultPositiveAngle.Value = 360f +_signedAngle;
}else{
resultNegativeAngle.Value = -360f+_signedAngle;
resultPositiveAngle.Value = _signedAngle;
}

}
}
}

Thanks!

Kubold

  • Full Member
  • ***
  • Posts: 112
Re: Get Signed Angle To Target, but with X axis
« Reply #1 on: January 24, 2014, 09:15:59 AM »
I worked around it.

I used Get Angle To Target (the regular, nearest angle, not signed angle). Then, if the target's World Y position is higher than the GameObject's we measure from, I multiply the angle by 1. If it's lower, I multiply by -1. And I have a signed angle.

A proper action would be nice though :)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Signed Angle To Target, but with X axis
« Reply #2 on: January 27, 2014, 04:59:38 AM »
Hi,

 Yes, good point, I'll try to make an update on this, to choose any axis.

bye,

 Jean

MS80

  • Junior Playmaker
  • **
  • Posts: 64
Re: Get Signed Angle To Target, but with X axis
« Reply #3 on: July 23, 2014, 12:13:03 PM »
Hi,

is there a update (get signed angle to target any axis) available?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Signed Angle To Target, but with X axis
« Reply #4 on: July 30, 2014, 05:51:13 AM »
Hi,

 not yet, I'll make a note on this thread when it will be done.

 Bye,

 Jean

Thyriax

  • Playmaker Newbie
  • *
  • Posts: 25
    • Torii games
Re: Get Signed Angle To Target, but with X axis
« Reply #5 on: August 26, 2014, 03:52:32 AM »
Hi Jean!

Any news about this action?
Is it still in dev?

Thanks!
bye!

TrentSterling

  • Junior Playmaker
  • **
  • Posts: 89
  • Someday I'll make games!
    • My Blog
Re: Get Signed Angle To Target, but with X axis
« Reply #6 on: September 21, 2014, 10:29:39 AM »
Bump. This would be a nice action to have updated for all axis.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Signed Angle To Target, but with X axis
« Reply #7 on: November 21, 2014, 03:36:36 AM »
Hi,

 It's now on the Ecosystem.

it now lets you select the axis, as well as inverting the sign, which is useful depending on what you do or even in game to switch.

GetSignedAngletoTarget

 Bye,

 Jean

TrentSterling

  • Junior Playmaker
  • **
  • Posts: 89
  • Someday I'll make games!
    • My Blog
Re: Get Signed Angle To Target, but with X axis
« Reply #8 on: December 14, 2014, 09:44:43 PM »
Thank you so much Jean!!

RaHaN

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Get Signed Angle To Target, but with X axis
« Reply #9 on: August 24, 2016, 11:58:26 AM »
Hello everyone !

I'm using this action in my game as well, for aiming purposes. However, I have an issue I can't seem to figure out.

First, let me explain my basic setup, visually (click the image for full view):



It's a pretty straightforward way to handle tracking of the aiming direction (i.e. where does the camera point to).

Here is my object hierarchy (a state machine handles the camera following the player object, as the Camera Rig can't be parented to it for 3C's reasons).



Some additional behaviours to better understand my setup: when the player Aims (right mouse button), the player doesn't rotate anymore on the Y axis. The Animator blends additional animations on the upper body to follow the aiming angle on the X and Y directions.

The variables "camXAngle" and "camYAngle" feed the animator (respectively HorAimAngle and VerAimAngle parameters) as shown below (click the image for full view):


However, when camXAngle is starting to depart a lot from its origin, camYAngle starts to go up - and that's what I don't understand.

Here is a video showing the issue at run time:


Changing the position of the aimCompass object doesn't solve the issue, it does moves it to another part of the Y axis rotation.

Is this normal behaviour for the Get Signed Angle action?
« Last Edit: August 24, 2016, 12:08:33 PM by RaHaN »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Signed Angle To Target, but with X axis
« Reply #10 on: August 29, 2016, 07:37:18 AM »
Hi,

 Please don't double post, instead, make a reference to your other post from there.

Thanks :)

 Bye,

 Jean