Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: speedything on December 16, 2011, 01:39:02 PM

Title: Get Random Point On Sphere
Post by: speedything on December 16, 2011, 01:39:02 PM
Hi all,

First post and just want to say that I'm absolutely loving this add-on. I'm Design Director at my company and next year I'm going to try to roll it out to my guys to use for prototyping.

Anyway, here's an action I just had to add for my own project. It will create a random point on a fictional sphere's surface which can be stored either as a Vector3 or as XYZ floats.

Code: [Select]
using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Vector3)]
[Tooltip("Get Random Vector3 from the surface of a radius 1 Sphere")]
public class GetRandomPointOnSphere : FsmStateAction
{

[UIHint(UIHint.Variable)]
public FsmVector3 vector;

[UIHint(UIHint.Variable)]
public FsmFloat x;

[UIHint(UIHint.Variable)]
public FsmFloat y;

[UIHint(UIHint.Variable)]
public FsmFloat z;

public bool everyFrame;

public override void Reset()
{
vector = null;
x = null;
y = null;
z = null;
everyFrame = false;
}

public override void OnEnter()
{
DoGetRandomPointOnSphere();

if (!everyFrame)
Finish();
}

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

void DoGetRandomPointOnSphere()
{
Vector3 position;

position = Random.onUnitSphere;

vector.Value = position;
x.Value = position.x;
y.Value = position.y;
z.Value = position.z;
}


}
}

It's pretty basic code that simply calls Unity's Random.onUnitSphere. Unfortunately the sphere has to be of radius 1 but you can then do stuff with the floats as a second step.

Bye,

Title: Re: Get Random Point On Sphere
Post by: Alex Chouls on December 17, 2011, 10:38:18 AM
Cool! Would be convenient to set a radius right in this action...
Title: Re: Get Random Point On Sphere
Post by: Andrew.Lukasik on January 02, 2012, 12:03:09 PM
Hi
Can you describe one or two situations in which this script comes most handy for you?

Thanks for sharing!
Andrew
Title: Re: Get Random Point On Sphere
Post by: speedything on January 06, 2012, 10:11:53 PM
The situation I'm using it for is to generate an asteroid belt around a sun. I get my random point, do a little multiplication on the X and Z, and then instantiate an asteroid at the location.

Most other situations I can think of also involve spawning, but I can imagine it could be used for other things. Maybe generating a random 3D movement direction or similar?
Title: Re: Get Random Point On Sphere
Post by: elmoeduardo on October 21, 2014, 03:22:10 AM
Started with your code and upgraded it a little.
i actually use it to set random wandering points for an enemy within a certain radius, and it also translates that point to the sphere origin. Hope it helps somebody :) 
Code: [Select]
using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Vector3)]
[Tooltip("Get Random Vector3 from a sphere")]
public class GetRandomPointOnSphere : FsmStateAction
{

[UIHint(UIHint.Variable)]
public FsmVector3 sphereCenter;

[UIHint(UIHint.Variable)]
public FsmFloat sphereRadius;

[UIHint(UIHint.Variable)]
public FsmVector3 storeResult;

public bool everyFrame;

public override void Reset()
{
sphereCenter = null;
sphereRadius = new FsmFloat { UseVariable = true };
storeResult = null;
everyFrame = false;
}

public override void OnEnter()
{
DoGetRandomPointOnSphere();

if (!everyFrame)
Finish();
}

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

void DoGetRandomPointOnSphere()
{
Vector3 position;

position = Random.onUnitSphere;
position *= sphereRadius.Value;
position += sphereCenter.Value;

storeResult.Value = position;

}


}
}
Title: Re: Get Random Point On Sphere
Post by: jeanfabre on April 13, 2016, 03:55:27 AM
Hi,

 Used them actions to make two actions on the Ecosystem (http://j.mp/PlayMakerEcosystem) ( with advanced feature for update, fixed and lateupdate feature).

https://twitter.com/JeanAtPlayMaker/status/720155971356647425


Bye,

 Jean