playMaker

Author Topic: Set a random direction and move in that direction  (Read 3786 times)

centaurianmudpig

  • Playmaker Newbie
  • *
  • Posts: 10
Set a random direction and move in that direction
« on: July 30, 2015, 11: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.

Thanks for reading.

phannDOTde

  • Full Member
  • ***
  • Posts: 237
    • Peter Hann .de
Re: Set a random direction and move in that direction
« Reply #1 on: July 31, 2015, 08: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?

centaurianmudpig

  • Playmaker Newbie
  • *
  • Posts: 10
Re: Set a random direction and move in that direction
« Reply #2 on: August 01, 2015, 06: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?

centaurianmudpig

  • Playmaker Newbie
  • *
  • Posts: 10
Re: Set a random direction and move in that direction
« Reply #3 on: August 05, 2015, 06: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();
    }
    }
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Set a random direction and move in that direction
« Reply #4 on: August 15, 2015, 05:50:14 PM »
Hi,

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

 Bye,

 Jean