playMaker

Author Topic: Can someone PLEASE modified this action "lookat2d"  (Read 2753 times)

RC

  • Full Member
  • ***
  • Posts: 148
Can someone PLEASE modified this action "lookat2d"
« on: December 02, 2014, 10:01:05 PM »
Can someone please modified this action. I tried to do it myself but fail to get it right.
Instead of rotating Z axis, Can anyone change it to rotating the Y axis instead??

Thank you!!

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2014. All rights reserved.
// original action created by collidernyc: http://hutonggames.com/playmakerforum/index.php?topic=7075.msg37373#msg37373
//--- __ECO__ __ACTION__ ---//


using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Rotates a 2d Game Object on it's z axis so its forward vector points at a Target.")]
public class LookAt2dGameObject : FsmStateAction
{
[RequiredField]
[Tooltip("The GameObject to rotate.")]
public FsmOwnerDefault gameObject;

[Tooltip("The GameObject to Look At.")]
public FsmGameObject targetObject;

[Tooltip("Set the GameObject starting offset. In degrees. 0 if your object is facing right, 180 if facing left etc...")]
public FsmFloat rotationOffset;

[Title("Draw Debug Line")]
[Tooltip("Draw a debug line from the GameObject to the Target.")]
public FsmBool debug;

[Tooltip("Color to use for the debug line.")]
public FsmColor debugLineColor;

[Tooltip("Repeat every frame.")]
public bool everyFrame = true;

private GameObject go;
private GameObject goTarget;
private Vector3 lookAtPos;



public override void Reset()
{
gameObject = null;
targetObject = null;
debug = false;
debugLineColor = Color.green;
everyFrame = true;
}

public override void OnEnter()
{
DoLookAt();

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

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

void DoLookAt()
{
go = Fsm.GetOwnerDefaultTarget(gameObject);
goTarget = targetObject.Value;

if (go == null || targetObject == null)
{
return;
}

Vector3 diff = goTarget.transform.position - go.transform.position;
diff.Normalize();

float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
go.transform.rotation = Quaternion.Euler(0f, 0f, rot_z - rotationOffset.Value);

if (debug.Value)
{
Debug.DrawLine(go.transform.position, goTarget.transform.position, debugLineColor.Value);
}

}

}
}
« Last Edit: December 02, 2014, 10:03:07 PM by rongconcrx »

Sly

  • Full Member
  • ***
  • Posts: 127
Re: Can someone PLEASE modified this action "lookat2d"
« Reply #1 on: December 03, 2014, 09:08:13 AM »
There is the code:

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2014. All rights reserved.
// original action created by collidernyc: http://hutonggames.com/playmakerforum/index.php?topic=7075.msg37373#msg37373
//--- __ECO__ __ACTION__ ---//
//Sly Guy was here for a little change ;)

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Rotates a 2d Game Object on it's y axis so its forward vector points at a Target.")]
public class LookAt2DCustom : FsmStateAction
{
[RequiredField]
[Tooltip("The GameObject to rotate.")]
public FsmOwnerDefault gameObject;

[Tooltip("The GameObject to Look At.")]
public FsmGameObject targetObject;

[Tooltip("Set the GameObject starting offset. In degrees. 0 if your object is facing right, 180 if facing left etc...")]
public FsmFloat rotationOffset;

[Title("Draw Debug Line")]
[Tooltip("Draw a debug line from the GameObject to the Target.")]
public FsmBool debug;

[Tooltip("Color to use for the debug line.")]
public FsmColor debugLineColor;

[Tooltip("Repeat every frame.")]
public bool everyFrame = true;

private GameObject go;
private GameObject goTarget;
private Vector3 lookAtPos;



public override void Reset()
{
gameObject = null;
targetObject = null;
debug = false;
debugLineColor = Color.green;
everyFrame = true;
}

public override void OnEnter()
{
DoLookAt();

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

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

void DoLookAt()
{
go = Fsm.GetOwnerDefaultTarget(gameObject);
goTarget = targetObject.Value;

if (go == null || targetObject == null)
{
return;
}

Vector3 diff = goTarget.transform.position - go.transform.position;
diff.Normalize();

float rot_y = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
go.transform.rotation = Quaternion.Euler(0f, rot_y - rotationOffset.Value, 0f );

if (debug.Value)
{
Debug.DrawLine(go.transform.position, goTarget.transform.position, debugLineColor.Value);
}

}

}
}

Just change the name of your file to LookAt2DCustom or change the name of the action to the same name as your file.

Just let me know if it's doing exactly what you want.

RC

  • Full Member
  • ***
  • Posts: 148
Re: Can someone PLEASE modified this action "lookat2d"
« Reply #2 on: December 03, 2014, 10:04:10 AM »
Thank you so much for taking your time to modified this.
Work perfectly.

Thanks again!

Sly

  • Full Member
  • ***
  • Posts: 127
Re: Can someone PLEASE modified this action "lookat2d"
« Reply #3 on: December 03, 2014, 12:17:53 PM »
You're welcome!
When I saw people taking time to help me, it's natural to take time for other too!
Enjoy!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: Can someone PLEASE modified this action "lookat2d"
« Reply #4 on: December 05, 2014, 01:39:05 AM »
Hi,

 It feels good to see such positive spirit around :)

 Bye,

 Jean