Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: Jake on December 11, 2012, 04:14:55 AM

Title: Random Vector2 / Vector3
Post by: Jake on December 11, 2012, 04: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
Title: Re: Random Vector2 / Vector3
Post by: Damian on December 11, 2012, 05:19:05 AM
Why not use a random float and put that in vector 2 or 3?
Title: Re: Random Vector2 / Vector3
Post by: Jake on December 11, 2012, 08: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
Title: Re: Random Vector2 / Vector3
Post by: Damian on December 11, 2012, 08: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.

Title: Re: Random Vector2 / Vector3
Post by: Jake on December 11, 2012, 08: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
Title: Re: Random Vector2 / Vector3
Post by: Rakusta on January 14, 2013, 10:22:05 PM
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();
        }
    }
}
Title: Re: Random Vector2 / Vector3
Post by: doppelmonster on February 03, 2014, 10:40:44 AM
Thanks for sharing! Its real time saver instead of breaking things up in random floats!
Title: Re: Random Vector2 / Vector3
Post by: MajorIdea on August 01, 2014, 10:52:18 AM
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?
Title: Re: Random Vector2 / Vector3
Post by: jeanfabre on August 01, 2014, 11:12:30 AM
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
Title: Re: Random Vector2 / Vector3
Post by: wrongtarget on March 24, 2015, 09:22:53 AM
I believe these were never added to the ecosystem?
Title: Re: Random Vector2 / Vector3
Post by: jeanfabre on March 25, 2015, 11:14:19 PM
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 (https://github.com/jeanfabre/PlayMakerCustomActions_U3/blob/master/Assets/PlayMaker%20Custom%20Actions/Vector2/RandomVector2.cs)

RandomVector3 (https://github.com/jeanfabre/PlayMakerCustomActions_U3/blob/master/Assets/PlayMaker%20Custom%20Actions/Vector3/RandomVector3.cs)

Bye,

 Jean
Title: Re: Random Vector2 / Vector3
Post by: craigz on July 11, 2016, 05: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
Title: Re: Random Vector2 / Vector3
Post by: jeanfabre on July 22, 2016, 12: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