playMaker

Author Topic: Arcing Projectiles To Target Position  (Read 3647 times)

RC

  • Full Member
  • ***
  • Posts: 148
Arcing Projectiles To Target Position
« 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/ 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);
        }
    }
}



« Last Edit: April 03, 2020, 09:04:11 PM by rongconcrx »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Arcing Projectiles To Target Position
« Reply #1 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

RC

  • Full Member
  • ***
  • Posts: 148
Re: Arcing Projectiles To Target Position
« Reply #2 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!

craigz

  • Beta Group
  • Full Member
  • *
  • Posts: 234
    • Haven Made
Re: Arcing Projectiles To Target Position
« Reply #3 on: April 21, 2020, 06:11:15 PM »
This is excellent rongconcrx! Thank your for sharing :D

-craigz

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Arcing Projectiles To Target Position
« Reply #4 on: April 22, 2020, 02:16:29 AM »
Hi,

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

Bye,

 Jean

RC

  • Full Member
  • ***
  • Posts: 148
Re: Arcing Projectiles To Target Position
« Reply #5 on: August 19, 2020, 04:39:41 PM »
Hi Jean. here is a test scene

Wrensey

  • Playmaker Newbie
  • *
  • Posts: 23
Re: Arcing Projectiles To Target Position
« Reply #6 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?
Follow me on twitter!

RC

  • Full Member
  • ***
  • Posts: 148
Re: Arcing Projectiles To Target Position
« Reply #7 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.

hoyoyo80

  • Full Member
  • ***
  • Posts: 136
Re: Arcing Projectiles To Target Position
« Reply #8 on: September 12, 2020, 09:51:51 PM »
Is there anyway i can get this action?Really need it,Thanks!!!!

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Arcing Projectiles To Target Position
« Reply #9 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.

hoyoyo80

  • Full Member
  • ***
  • Posts: 136
Re: Arcing Projectiles To Target Position
« Reply #10 on: September 21, 2020, 07:03:16 AM »
@djaydino.Thank a lot!!! ill try that.