playMaker

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

Six66

  • Full Member
  • ***
  • Posts: 135
    • View Profile
Arcing Projectiles To Target Position
« on: April 03, 2020, 04: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, 06:04:11 PM by rongconcrx »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15330
  • Official Playmaker Support
    • View Profile
Re: Arcing Projectiles To Target Position
« Reply #1 on: April 06, 2020, 12: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

Six66

  • Full Member
  • ***
  • Posts: 135
    • View Profile
Re: Arcing Projectiles To Target Position
« Reply #2 on: April 06, 2020, 03: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: 207
    • View Profile
    • Haven Made
Re: Arcing Projectiles To Target Position
« Reply #3 on: April 21, 2020, 03:11:15 PM »
This is excellent rongconcrx! Thank your for sharing :D

-craigz

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15330
  • Official Playmaker Support
    • View Profile
Re: Arcing Projectiles To Target Position
« Reply #4 on: April 21, 2020, 11:16:29 PM »
Hi,

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

Bye,

 Jean