Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: RC on April 03, 2020, 07:20:54 PM

Title: Arcing Projectiles To Target Position
Post by: RC on April 03, 2020, 07:20:54 PM
Hello, I just finish converting this scrip to Playmaker action and would like to share it with everyone. the script is from http://luminaryapps.com/blog/arcing-projectiles-in-unity/ (http://luminaryapps.com/blog/arcing-projectiles-in-unity/) and they are much appreciated!

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{

    [ActionCategory("Custom")]
    public class Projectile_to_target : FsmStateAction
    {

        [Tooltip("Position we want to hit")]
        public FsmVector3 targetPos;

        [Tooltip("Horizontal speed, in units/sec")]
        public FsmFloat speed = 10;

        [Tooltip("How high the arc should be, in units")]
        public FsmFloat arcHeight = 1;

        public FsmVector3 nextPos;


        public FsmVector3 startPos;

        FsmFloat archfloat = -0.25f;

        [Tooltip("Repeat every frame.")]
        public bool everyFrame = true;

        // Code that runs on entering the state.
        public override void OnEnter()
        {
            // Cache our start position, which is really the only thing we need
            // (in addition to our current position, and the target).
            startPos.Value = Fsm.GameObject.transform.position;
        }


        public override void OnUpdate()
        {
            // Compute the next position, with arc added in
            FsmFloat x0 = startPos.Value.x;
            FsmFloat x1 = targetPos.Value.x;
            FsmFloat dist = x1.Value - x0.Value;
            FsmFloat nextX = Mathf.MoveTowards(Fsm.GameObject.transform.position.x, x1.Value, speed.Value * Time.deltaTime);
            FsmFloat baseY = Mathf.Lerp(startPos.Value.y, targetPos.Value.y, (nextX.Value - x0.Value) / dist.Value);
            FsmFloat arc = arcHeight.Value * (nextX.Value - x0.Value) * (nextX.Value - x1.Value) / (archfloat.Value * dist.Value * dist.Value);
            nextPos = new Vector3(nextX.Value, baseY.Value + arc.Value, Fsm.GameObject.transform.position.z);

            // Rotate to face the next position, and then move there
            Fsm.GameObject.transform.rotation = LookAt2D(nextPos.Value - Fsm.GameObject.transform.position);
            Fsm.GameObject.transform.position = nextPos.Value;
        }

        void Arrived()
        {

        }

        ///
        /// This is a 2D version of Quaternion.LookAt; it returns a quaternion
        /// that makes the local +X axis point in the given forward direction.
        ///
        /// forward direction
        /// Quaternion that rotates +X to align with forward
        static Quaternion LookAt2D(Vector2 forward)
        {
            return Quaternion.Euler(0, 0, Mathf.Atan2(forward.y, forward.x) * Mathf.Rad2Deg);
        }
    }
}

(http://i.imgur.com/bxbvvmV.gif)

Title: Re: Arcing Projectiles To Target Position
Post by: jeanfabre on April 06, 2020, 03:15:26 AM
Hi,

 Very nice indeed! Few errors and things I'll correct before I put it on the Ecosystem, should be done this week.

Bye,

 Jean
Title: Re: Arcing Projectiles To Target Position
Post by: RC on April 06, 2020, 06:19:36 PM
Hi,

 Very nice indeed! Few errors and things I'll correct before I put it on the Ecosystem, should be done this week.

Bye,

 Jean

thank you!
Title: Re: Arcing Projectiles To Target Position
Post by: craigz on April 21, 2020, 06:11:15 PM
This is excellent rongconcrx! Thank your for sharing :D

-craigz
Title: Re: Arcing Projectiles To Target Position
Post by: jeanfabre on April 22, 2020, 02:16:29 AM
Hi,

Could you share your test scene, I had trouble making it work.

Bye,

 Jean
Title: Re: Arcing Projectiles To Target Position
Post by: RC on August 19, 2020, 04:39:41 PM
Hi Jean. here is a test scene
Title: Re: Arcing Projectiles To Target Position
Post by: Wrensey on August 19, 2020, 05:03:26 PM
I was just looking for something like this, nice work and thanks for sharing! Does this only work in 2D?
Title: Re: Arcing Projectiles To Target Position
Post by: RC on August 19, 2020, 06:29:12 PM
I was just looking for something like this, nice work and thanks for sharing! Does this only work in 2D?

thanks! yes, it's only work in for in 2d.
Title: Re: Arcing Projectiles To Target Position
Post by: hoyoyo80 on September 12, 2020, 09:51:51 PM
Is there anyway i can get this action?Really need it,Thanks!!!!
Title: Re: Arcing Projectiles To Target Position
Post by: djaydino on September 20, 2020, 04:46:27 AM
Hi.
i have something similar, try the attachment below.
it has an action and a Rigidbody2DExtensions script.

it has a range checker build in which will send an event if the target point can not be reached.
Title: Re: Arcing Projectiles To Target Position
Post by: hoyoyo80 on September 21, 2020, 07:03:16 AM
@djaydino.Thank a lot!!! ill try that.