Playmaker Forum
PlayMaker Updates & Downloads => Share New Actions => Topic started 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.
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,
-
Cool! Would be convenient to set a radius right in this action...
-
Hi
Can you describe one or two situations in which this script comes most handy for you?
Thanks for sharing!
Andrew
-
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?
-
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 :)
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;
}
}
}
-
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