playMaker

Author Topic: Random Vector2 / Vector3  (Read 10051 times)

Jake

  • Junior Playmaker
  • **
  • Posts: 61
    • Fluffy Underware
Random Vector2 / Vector3
« on: December 11, 2012, 06:14:55 AM »
Hi there,

first I'd like to say that I'm a new playMaker user and the more I see, the more I love it. Also I've planned to add playMaker support to my products (mainly Magical Box and Measure It! Pro) in the near future.

Here are some actions I've missed covering Unity's Random class:

RandomVector2:
Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Math)]
    [Tooltip("Creates a random Vector2 inside a unit circle")]
    public class RandomVector2 : FsmStateAction
    {
        [UIHint(UIHint.Variable)]
        public FsmVector2 storeResult;

        public override void Reset()
        {
            storeResult = null;
        }

        public override void OnEnter()
        {
            storeResult.Value = Random.insideUnitCircle;
            Finish();
        }
    }
}

RandomVector3:
Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Math)]
    [Tooltip("Creates a random Vector3 inside a unit sphere")]
    public class RandomVector3 : FsmStateAction
    {
        [UIHint(UIHint.Variable)]
        public FsmVector3 storeResult;

        public override void Reset()
        {
            storeResult = null;
        }

        public override void OnEnter()
        {
            storeResult.Value = Random.insideUnitSphere;
            Finish();
        }
    }
}

More to come...

Cheers
Jake

Damian

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 188
    • Permaximum Betty
Re: Random Vector2 / Vector3
« Reply #1 on: December 11, 2012, 07:19:05 AM »
Why not use a random float and put that in vector 2 or 3?

Jake

  • Junior Playmaker
  • **
  • Posts: 61
    • Fluffy Underware
Re: Random Vector2 / Vector3
« Reply #2 on: December 11, 2012, 10:02:22 AM »
Hm,

maybe I've missed something, but how do you create a random Vector3 by putting a random Float in it? Say you want to position a gameobject somewhere inside a sphere...

Jake

Damian

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 188
    • Permaximum Betty
Re: Random Vector2 / Vector3
« Reply #3 on: December 11, 2012, 10:20:42 AM »
Hm,

maybe I've missed something, but how do you create a random Vector3 by putting a random Float in it? Say you want to position a gameobject somewhere inside a sphere...

Jake

If you know the x.y.z on that sphere you can take those  values and use random float from them and use those x.y.z on the gameobject.


Jake

  • Junior Playmaker
  • **
  • Posts: 61
    • Fluffy Underware
Re: Random Vector2 / Vector3
« Reply #4 on: December 11, 2012, 10:36:20 AM »
Ok, but that looks more like a workaround than a good solution to me. Nevertheless, it was easy to create those actions and I've found them possible useful for others, so I've shared them.

Jake

Rakusta

  • Playmaker Newbie
  • *
  • Posts: 2
Re: Random Vector2 / Vector3
« Reply #5 on: January 15, 2013, 12:22:05 AM »
Added variable input for UnitSphere's radius, here it be!

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Math)]
    [Tooltip("Creates a random Vector3 inside a unit sphere")]
    public class RandomVector3 : FsmStateAction
    {
        [UIHint(UIHint.Variable)]
        public FsmVector3 storeResult;

[Tooltip("Radius of imaginary sphere")]
public FsmFloat unitSphere;

        public override void Reset()
        {
            storeResult = null;
            unitSphere = null;
        }

        public override void OnEnter()
        {
            storeResult.Value = Random.insideUnitSphere*unitSphere.Value;
            Finish();
        }
    }
}

doppelmonster

  • Full Member
  • ***
  • Posts: 157
    • Grinder Games
Re: Random Vector2 / Vector3
« Reply #6 on: February 03, 2014, 12:40:44 PM »
Thanks for sharing! Its real time saver instead of breaking things up in random floats!

MajorIdea

  • Full Member
  • ***
  • Posts: 131
Re: Random Vector2 / Vector3
« Reply #7 on: August 01, 2014, 01:52:18 PM »
Thanks, but am I wrong, or is the UnitSphere always positioned at the center of the world? Shouldn't it be created relative to the Object that spawns it?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Random Vector2 / Vector3
« Reply #8 on: August 01, 2014, 02:12:30 PM »
Hi,

 you have a good point, but that would be an option, because it's very often the case that you want to have a random unit vector without caring of the actual gameobject, this is the case for all "managers" and logic features that must be attached to a GameObject because Unity made it so, which is beyond me to be honest... I don't see the point of having a transform on a GameObject that's not needed in the 3d world to begin with.

I also realized that them actions are not on the ecosystem, I'll add them next week with this option of being relative to a gameobject.


 Bye,

 Jean

wrongtarget

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Random Vector2 / Vector3
« Reply #9 on: March 24, 2015, 12:22:53 PM »
I believe these were never added to the ecosystem?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Random Vector2 / Vector3
« Reply #10 on: March 26, 2015, 02:14:19 AM »
Hi,

 Thanks for the reminder :)

I added several features
-- ability to set the seed for predictable results ( leave to none for general purpose)
-- everyFrame Option
-- option to get the random values inside the radius or on the perimeter/surface.

You can search and download them on the Ecosystem

sources:
RandomVector2

RandomVector3

Bye,

 Jean

craigz

  • Beta Group
  • Full Member
  • *
  • Posts: 234
    • Haven Made
Re: Random Vector2 / Vector3
« Reply #11 on: July 11, 2016, 08:56:30 PM »
just found these, super useful :D

question! Could the randomvector2 action get a gameobject/position variable input for Center as well as axis? :D

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Random Vector2 / Vector3
« Reply #12 on: July 22, 2016, 03:51:49 AM »
Hi,

 I don't understand your question. I guess a vector can both represent a center to position an object, OR a direction, it's up to you and doesn't change the principle of a vector.

 however, for a direction, you could normalize the random vector, so that the magnitude of the vector is 1, which is prefferable for a vector that expresses a direction. Does that make sense?

Bye,

 Jean