playMaker

Author Topic: SetLookRotation  (Read 3801 times)

Jake

  • Junior Playmaker
  • **
  • Posts: 61
    • Fluffy Underware
SetLookRotation
« on: January 28, 2013, 09:52:15 AM »
This action sets a Transform's rotation using Quaternion.LookRotation(). Maybe LookRotation can be added to the official SetRotation.cs (just like the euler option etc...), this would be more meaningful than another action for this.

Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker;

[ActionCategory(ActionCategory.Transform)]
[Tooltip("Sets the Rotation of a GameObject using Quaternion.LookRotation")]
public class SetLookRotation : FsmStateAction
{
    [RequiredField]
    [Tooltip("The GameObject to rotate.")]
    public FsmOwnerDefault gameObject;

    [Tooltip("The forward vector to look to")]
    public FsmVector3 forward;
    [Tooltip("the vector defining up")]
    public FsmVector3 upward;

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

    [Tooltip("Perform in LateUpdate. This is useful if you want to override the position of objects that are animated or otherwise positioned in Update.")]
    public bool lateUpdate;

// Code that runs on entering the state.
public override void OnEnter()
{
        if (!everyFrame && !lateUpdate) {
            DoSetRotation();
            Finish();
        }
}

// Code that runs every frame.
public override void OnUpdate()
{
        if (!lateUpdate)
            DoSetRotation();
}

public override void OnLateUpdate()
{
        if (lateUpdate) {
            DoSetRotation();
        }

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

    void DoSetRotation()
    {
        var go = Fsm.GetOwnerDefaultTarget(gameObject);
        if (go == null) {
            return;
        }
        go.transform.rotation=Quaternion.LookRotation(forward.Value, upward.Value);
    }

    public override void Reset()
    {
        base.Reset();
        gameObject = null;
        forward = new FsmVector3();
        upward = Vector3.up;
    }

}


Cheers
Jake