playMaker

Author Topic: Get Scale  (Read 3698 times)

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • View Profile
    • FluidPlay Studios
Get Scale
« on: April 05, 2011, 03:10:41 PM »
Hey it looks as I'm the first ;) C'mon coders, stop saving up stuff for yourselves and share the goodies! This is a little request from another thread, a missing lil' action to get the scale of a given game object. It returns the "lossy scale" if you use world, read about it here: http://unity3d.com/support/documentation/ScriptReference/Transform-lossyScale.html

To add it, just copy and paste the code below into a new C# script (I generally do it straight inside the Unity editor for consistency sake), name it as "GetScale.cs", save and you're done. It'll show up in the Transform category inside the action browser. Enjoy:

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Transform)]
    [Tooltip("Gets the Scale of a Game Object and stores it in a Vector3 Variable or each Axis in a Float Variable")]
    public class GetScale : FsmStateAction
    {
        [RequiredField]
        public FsmOwnerDefault gameObject;
        [UIHint(UIHint.Variable)]
        public FsmVector3 vector;
        [UIHint(UIHint.Variable)]
        public FsmFloat xScale;
        [UIHint(UIHint.Variable)]
        public FsmFloat yScale;
        [UIHint(UIHint.Variable)]
        public FsmFloat zScale;
        public Space space;
        public bool everyFrame;

        public override void Reset()
        {
            gameObject = null;
            vector = null;
            xScale = null;
            yScale = null;
            zScale = null;
            space = Space.World;
            everyFrame = false;
        }

        public override void OnEnter()
        {
            DoGetScale();

            if (!everyFrame)
                Finish();
        }

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

        void DoGetScale()
        {
            GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
            if (go == null) return;

            Vector3 scale;

            if (space == Space.World)
            {
                scale = go.transform.lossyScale;
            }
            else
            {
                scale = go.transform.localScale;
            }

            vector.Value = scale;
            xScale.Value = scale.x;
            yScale.Value = scale.y;
            zScale.Value = scale.z;
        }


    }
}
--
Breno "MaDDoX" Azevedo
@brenoazevedo

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
    • View Profile
Re: Get Scale
« Reply #1 on: April 05, 2011, 03:47:43 PM »
Thank you!

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3201
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: Get Scale
« Reply #2 on: April 05, 2011, 09:30:33 PM »
Thanks MaDDoX!!  ;D

flekoun

  • Playmaker Newbie
  • *
  • Posts: 19
    • View Profile
Re: Get Scale
« Reply #3 on: April 05, 2011, 09:49:51 PM »
Thanks a lot! Was looking for it!

Steamgauge

  • Playmaker Newbie
  • *
  • Posts: 4
    • View Profile
    • Steamgauge
Re: Get Scale
« Reply #4 on: April 06, 2011, 05:08:01 AM »
Nice one MaDDoX! Thank you!
-