playMaker

Author Topic: Get Random Point On Sphere  (Read 7568 times)

speedything

  • Playmaker Newbie
  • *
  • Posts: 13
Get Random Point On Sphere
« 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,


Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4000
  • Official Playmaker Support
    • LinkedIn
Re: Get Random Point On Sphere
« Reply #1 on: December 17, 2011, 10:38:18 AM »
Cool! Would be convenient to set a radius right in this action...

Andrew.Lukasik

  • Full Member
  • ***
  • Posts: 134
    • my twitter @andrewlukasik
Re: Get Random Point On Sphere
« Reply #2 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

speedything

  • Playmaker Newbie
  • *
  • Posts: 13
Re: Get Random Point On Sphere
« Reply #3 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?

elmoeduardo

  • Playmaker Newbie
  • *
  • Posts: 2
Re: Get Random Point On Sphere
« Reply #4 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;

}


}
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Random Point On Sphere
« Reply #5 on: April 13, 2016, 03:55:27 AM »
Hi,

 Used them actions to make two actions on the Ecosystem ( with advanced feature for update, fixed and lateupdate feature).

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


Bye,

 Jean