Join our Discord Channel
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); } }}
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
I was just looking for something like this, nice work and thanks for sharing! Does this only work in 2D?