playMaker

Author Topic: Look At and Related Actions for Unity 2D  (Read 12510 times)

Phillament

  • Playmaker Newbie
  • *
  • Posts: 44
Look At and Related Actions for Unity 2D
« on: December 18, 2013, 04:38:07 AM »
Hi,

I've been trying to use the current Look At actions and nothing works for me, since I'm using Unity 2D. I was told that forward in Unity is in the Z axis, for whatever reason, and so this really clashes with the Unity 2D  set-up. I think Playmaker needs an update for a 2D version of the Look At (and related) actions, just to maximize it's value and usability. I hope this isn't too much to ask.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15594
  • Official Playmaker Support
Re: Look At and Related Actions for Unity 2D
« Reply #1 on: December 19, 2013, 02:36:50 AM »
Hi,

 that's a good point, as did not think of them actions when I ported unity 2d, I think it makes a lot of sense to have these specific actions reworked for unity 2d.

bye,

 Jean

Phillament

  • Playmaker Newbie
  • *
  • Posts: 44
Re: Look At and Related Actions for Unity 2D
« Reply #2 on: December 19, 2013, 03:23:30 AM »
Hi,

 that's a good point, as did not think of them actions when I ported unity 2d, I think it makes a lot of sense to have these specific actions reworked for unity 2d.

bye,

 Jean

Cool, I'm glad you agree! This would make my project a looot better, hahah. I hope it's not too tough to do. When do you think this updated could be in?

Thanks!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15594
  • Official Playmaker Support
Re: Look At and Related Actions for Unity 2D
« Reply #3 on: December 19, 2013, 04:56:07 AM »
hi,


simply bump this thread early january if you don't hear from me before.

bye,

jean

Phillament

  • Playmaker Newbie
  • *
  • Posts: 44
Re: Look At and Related Actions for Unity 2D
« Reply #4 on: December 19, 2013, 03:20:49 PM »
All right, will do! Thanks, Jean!

dasbin

  • Junior Playmaker
  • **
  • Posts: 92
Re: Look At and Related Actions for Unity 2D
« Reply #5 on: December 19, 2013, 08:54:03 PM »
After much experimentation I modified the LookAt action using the following code which seems to work quite well.

Code: [Select]
Vector3 lookAtPos;
if (goTarget != null)
{
lookAtPos = goTarget.transform.position;
}
else
{
lookAtPos = targetPosition.Value;
}


lookAtPos.z = go.transform.position.z;

go.transform.up = (lookAtPos - go.transform.position);

Phillament

  • Playmaker Newbie
  • *
  • Posts: 44
Re: Look At and Related Actions for Unity 2D
« Reply #6 on: December 21, 2013, 03:40:42 AM »
Cool, how would I go about using this with PlayMaker actions? Do I just go into the related c# script and add those lines of code? I don't want to accidentally break the current action, for when I do need the 3D Look At functionality. Again, thanks for bothering!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15594
  • Official Playmaker Support
Re: Look At and Related Actions for Unity 2D
« Reply #7 on: December 21, 2013, 05:21:01 AM »
Hi,

 It's always best to make a new action with a unique name, editing existing official action is very dangerous cause the next time you will update playmaker, your changes will be overwritten and your project will bug.

 So I would duplicate the look at action, rename it's file and class to 2dLookAt or something and apply the changes described here.

bye,

 Jean

Phillament

  • Playmaker Newbie
  • *
  • Posts: 44
Re: Look At and Related Actions for Unity 2D
« Reply #8 on: December 22, 2013, 08:24:46 PM »
So I'm trying to integrate this code into a duplicate of the Look At behaviour, but I have no idea what I'm doing. Where exactly am I to place this code? I don't know where it fits in the original action. This is what I've got so far:

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Rotates a Game Object so its forward vector points at a Target. The Target can be specified as a GameObject or a world Position. If you specify both, then Position specifies a local offset from the target object's Position.")]
public class LookAt2D : FsmStateAction
{
[RequiredField]
[Tooltip("The GameObject to rotate.")]
public FsmOwnerDefault gameObject;

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

[Tooltip("World position to look at, or local offset from Target Object if specified.")]
public FsmVector3 targetPosition;

[Tooltip("Rotate the GameObject to point its up direction vector in the direction hinted at by the Up Vector. See Unity Look At docs for more details.")]
public FsmVector3 upVector;

[Tooltip("Don't rotate vertically.")]
public FsmBool keepVertical;

[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;
if (goTarget != null)
{
lookAtPos = goTarget.transform.position;
}
else
{
lookAtPos = targetPosition.Value;
}


lookAtPos.z = go.transform.position.z;

go.transform.up = (lookAtPos - go.transform.position);
   
private Vector3 lookAtPosWithVertical;
       
public override void Reset()
{
gameObject = null;
targetObject = null;
targetPosition = new FsmVector3 { UseVariable = true};
upVector = new FsmVector3 { UseVariable = true};
keepVertical = true;
debug = false;
debugLineColor = Color.yellow;
everyFrame = true;
}


public override void OnEnter()
{
DoLookAt();

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

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

void DoLookAt()
{
if (!UpdateLookAtPosition())
{
    return;
}

go.transform.LookAt(lookAtPos, upVector.IsNone ? Vector3.up : upVector.Value);

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

        public bool UpdateLookAtPosition()
        {
            go = Fsm.GetOwnerDefaultTarget(gameObject);
            if (go == null)
            {
                return false;
            }

            goTarget = targetObject.Value;
            if (goTarget == null && targetPosition.IsNone)
            {
                return false;
            }

            if (goTarget != null)
            {
                lookAtPos = !targetPosition.IsNone ? goTarget.transform.TransformPoint(targetPosition.Value) : goTarget.transform.position;
            }
            else
            {
                lookAtPos = targetPosition.Value;
            }

            lookAtPosWithVertical = lookAtPos;

            if (keepVertical.Value)
            {
                lookAtPos.y = go.transform.position.y;
            }

            return true;
        }

        public Vector3 GetLookAtPosition()
        {
            return lookAtPos;
        }

        public Vector3 GetLookAtPositionWithVertical()
        {
            return lookAtPosWithVertical;
        }
}
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15594
  • Official Playmaker Support
Re: Look At and Related Actions for Unity 2D
« Reply #9 on: December 26, 2013, 09:22:10 AM »
Hi,

 I have added this as a trello task:

https://trello.com/c/jLaS9RvH/42-unity-2d-look-at-and-related-actions-rework

 Don't forget to vote for this task ( using the vote button), It will help prioritize things.

bye,

 Jean

Phillament

  • Playmaker Newbie
  • *
  • Posts: 44
Re: Look At and Related Actions for Unity 2D
« Reply #10 on: December 27, 2013, 12:40:08 AM »
Awesome! Just voted for it! Thank you!

Phillament

  • Playmaker Newbie
  • *
  • Posts: 44
Re: Look At and Related Actions for Unity 2D
« Reply #11 on: January 20, 2014, 01:07:37 PM »
Hi,

How's the progress on this update going?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15594
  • Official Playmaker Support
Re: Look At and Related Actions for Unity 2D
« Reply #12 on: January 21, 2014, 03:33:54 AM »
Hi,

 It's still pending, hopefully, I will be able to put more time on clearing the tasks prior this one and get down to this.

bye,

 Jean