# Playmaker Forum

## PlayMaker Help & Tips => PlayMaker Help => Topic started by: centaurianmudpig on July 30, 2015, 08:40:12 AM

Title: Set a random direction and move in that direction
Post by: centaurianmudpig on July 30, 2015, 08:40:12 AM
Hello,

Trying to learn PlayMaker and figured I would do a simple Ping Pong clone.  Problem is I cannot figure out how to get the ball to move 10 pixels in a random direction, as the ball is being served.

I would like to attempt this by calculating the X and Y velocity of the ball, instead of rotating the sprite and making it move forward and not using physics.

Title: Re: Set a random direction and move in that direction
Post by: phannDOTde on July 31, 2015, 05:42:06 AM
how about making a helper object at the balls position, rotate the helper randomly and then add a phys force/impulse to the ball using the direction of the helper object?
Title: Re: Set a random direction and move in that direction
Post by: centaurianmudpig on August 01, 2015, 03:45:08 AM
I want to avoid using physics.   I'm searching for an action that will set a Vector2 calculated from a velocity and angle, if one exists?
Title: Re: Set a random direction and move in that direction
Post by: centaurianmudpig on August 05, 2015, 03:45:17 AM
I couldn't find an Action, so I created my own.  Possibly useful for others.

Code: [Select]
`using UnityEngine;namespace HutongGames.PlayMaker.Actions {    [ActionCategory("Vector2")]    [Tooltip("Calulates the 2D position from a given angle and length.")]    public class SetDistance2D : FsmStateAction    {        [UIHint(UIHint.Variable)]        [Tooltip("Required angle (theta) in degrees")]        public FsmFloat theta;        [Tooltip("Distance to move by")]        public FsmFloat distance;        [Tooltip("Store distance as a Vector2")]        public FsmVector2 fsmStoreVector2;        public FsmFloat fsmStoreX;        public FsmFloat fsmStoreY;        public override void Reset()        {            theta = null;            distance = null;        }        public override void OnEnter()     {            float rads = theta.Value * Mathf.PI / 180;            Vector2 newVector = new Vector2(                Mathf.Cos(rads) * distance.Value,                -Mathf.Sin(rads) * distance.Value                );            if(fsmStoreVector2.UsesVariable) fsmStoreVector2.Value = newVector;            if (fsmStoreX.UsesVariable) fsmStoreX.Value = newVector.x;            if (fsmStoreY.UsesVariable) fsmStoreY.Value = newVector.y;            Finish();     }    }}`
Title: Re: Set a random direction and move in that direction
Post by: jeanfabre on August 15, 2015, 02:50:14 PM
Hi,

Great! You should however allow for Everyframe execution, so that it can evolve over time, if needed.

Bye,

Jean