How to get a Projectile to follow a Parabola

Hi, i’m trying to implement several kinds of projectiles for the 2d scroller, and i thought a missile like projectile would be cool, but i’m getting some troubles to how to get the projectile follow a parabola trajectory, if the projecetiles where shooted in the X axis i think the unity physics engine would have been enough, but the
how can i get to shoot a single big curved projectile?

 IEnumerator FireContinuosly()
    {
        while (true)
        { 
          
            GameObject laser = Instantiate(
                laserPrefab, 
                transform.position, 
                Quaternion.identity) as GameObject; //default rotation
        laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed); 
           

        AudioSource.PlayClipAtPoint(fire_sound, Camera.main.transform.position, fireSoundVolume);
            yield return new WaitForSeconds(projectileFiringPeriod);

        }

this is the current code for the projectiles

Hi Shinos,

For simplicity, you could define a couple of “waypoints” of the parabola which the projectiles could follow. Those waypoints would be located relative to the spawn point.

1 Like

Mhh i see, so i could do like the first waypoint when i Instantiate is the spawn point and then i do a vector move towards the path i want, i’ll give it a try and i’ll tell if it worked! thank you

Hi, it’s me again, it looks like a i can’t figure it out how to solve this ç_ç, could you lend me a little hand?
i’m trying to understand how could i shoot a laser in a parabola or fixed path i choose but every thing i tried has been useless :sob: :sob: :sob:

Hey,

I would try to implement what you did for the enemy movement. So instead of creating a new vector for the velocity after instantiateing the laser, you need to use MoveTowards().
So I think you need a path gameObject and waypoint gameobjects as children. Then you need to put those waypoints in an array/list.
Just check the enemyPathing.cs.I think what we do there for enemy movement is pretty similar to what you are trying to achieve.

I’m trying mate but i keep getting mistakes, because the waypoints are fixed , so even if i get the projectile to follow the path it goes always in that direction, maybe i need to implement some deltaX and Y based on the spawn point of the player? I’m looking on how to do some parabola trajectory on the web but it’s a little complicated , i would like to understand how to do a proper parabola but i’m missing something

What mistakes? And what did you already accomplish? If you managed to make the projectiles follow a predefined path, your solution is almost complete. If you haven’t accomplished this yet, do that. Don’t try to make everything perfect. Make the projectile follow the path even if the path is fixed or if the result does not look good (yet).

actually i did managed to make them follow a path, wait i’ll show you a video a did

i kinda achieved something, i can get the projectile follow a path i want, but if i move the path doesn’t change dinamically i need to find a way to get the waypoint to change with the position of the player with the same offset
video:


i accomplished this by attaching a missilepath script to the single gameobject missile, which does a move toward to the game object when it gets instantiated by the player script when i Push a button, it’s a lil messy but it kinda works

as you can see at left i have those 3 dots which represent my path, but if i move my player they stay like that so the missiles goes to find that path wherever i am, i thought that i need to implement some kind of Offset based of where is the player, but i’m kinda confused right now ahhaha i need a bit of help
here is the code for the path in BossPath script which every missiles follow when it gets instantiated

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BossPathing : MonoBehaviour
{

    List<Transform> waypointsBoss;
    int waypointIndex = 0;
    float moveSpeed;

    void Start()
    {  
        waypointsBoss = FindObjectOfType<Boss>().GetWaypoints();
    }

    void Update()
    {
        Move();
    }

 
    private void Move()
    {//qui è dove una volta nato con boss.cs lo faccio muovere un poco, ho messo questo script direttamente sull'oggetto boss
        if (waypointIndex <= waypointsBoss.Count - 1)
        {
            var targetPosition = waypointsBoss[waypointIndex].transform.position + transform.position; //posizione target
            var movementThisFrame = 2f * Time.deltaTime; //velocità con la quale si muoverà
            transform.position = Vector2.MoveTowards //funzione per muoversi VERSO una posizione target
                (transform.position,
                targetPosition,
                movementThisFrame);

            if (transform.position == targetPosition)
            {
                waypointIndex++;
            }
        } //se sono all'ultimo 
   
    }

}

Thank you for the video. That’s exactly how it is supposed to look. Theoretically, all you need to do now is to make the projectiles move along the transform.localPosition of the waypoints plus the starting position (transform.position of the spawn point of the projectile). I’m not telling you how to implement this because I am sure you’ll solve this problem yourself. You are almost there. :slight_smile:

Of course, if you tried that and cannot complete the rest, let me know what you have, how the result looks and what is not working yet, and I’ll try to assist you.

Thank you! i’ll try this right now

i’m trying to implement the script changes you said but i think i’m missing something ahaha nothing changes when i shoot ç_ç

private void Move()
    {
        if (waypointIndex <= waypointsBoss.Count - 1)
        {
            var targetPosition = waypointsBoss[waypointIndex].transform.localPosition + transform.position ; 
            var movementThisFrame = 2f * Time.deltaTime; 
            transform.position = Vector2.MoveTowardstarget
                (transform.position,
                targetPosition,
                movementThisFrame);

            if (transform.position == targetPosition)
            {
                waypointIndex++;
            }
        } 
   
    }

did i put the transform.localPosition right?

You did but something crucial is missing.

Here is a visualisation of a path:

image

You are interested in the child positions relative to the parent position, thus in their respective localPosition.

However, the pivot point (“parent”) is not necessarily at (0, 0, 0) because the spawn point of the laser might be somewhere else. Nevertheless, the local positions of the children remain the same.

image

And this is how it should look when the projectile (star) is following the path:

image

As you can see, the “parent” position does not match the “projectile” position. The projectile follows the waypoints relative to “parent”.

Create 3 variables: startingPosition, waypointPosition, targetPosition.

And a hint:

startingPosition would be equal the “parent” position in my pictures.

1 Like

Wow you are really awesome, thank you :scream: i think i’m starting to understand, i’ll try this right now and i’ll let you know

Another hint in case it doesn’t work: Unity cannot remember anything, so one of the variables must be declared outside the method code block.

1 Like

Ok i feel really really dumb but everything i try literally gives me the same behavior :cold_sweat: i’m not able to manipulate the offset of the waypoints and i don’t know why, like i’m trying to give them an offset but when i play it changes nothing

Did you implement startingPosition? If so, share your entire BossPathing class again. I’m sure there is just a detail missing.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BossPathing : MonoBehaviour
{

    List<Transform> waypointsBoss;
    int waypointIndex = 0;
    float moveSpeed;
    Vector3 startingPosition;
   

    void Start()
    {  
        waypointsBoss = FindObjectOfType<Boss>().GetWaypoints();
    }

    void Update()
    {  
        startingPosition =FindObjectOfType<Player>().transform.position;

        Move();
    }

 
    private void Move()
    {//qui è dove una volta nato con boss.cs lo faccio muovere un poco, ho messo questo script direttamente sull'oggetto boss
        if (waypointIndex <= waypointsBoss.Count - 1)
        {   

            var targetPosition = waypointsBoss[waypointIndex].transform.localPosition + startingPosition; //posizione target
            var movementThisFrame = 2f * Time.deltaTime; //velocità con la quale si muoverà
            transform.position = Vector2.MoveTowards //funzione per muoversi VERSO una posizione target
                (transform.position,
                targetPosition,
                movementThisFrame);

            if (transform.position == targetPosition)
            {
                waypointIndex++;
            }
        } //se sono all'ultimo 
   
    }

}

i didn’t quite understood where i should use the targetposition and waypoint position variables you suggested, like i think i already have target position in the method (?)
i don’t know i’m just doing dumb mistakes i think

  1. Think again about startingPosition. Is it a position that changes while the projectile is moving? If so, why did I chose the name “startingPosition” instead of “currentPosition”?

  2. Where is the waypointPosition? And why would I suggest this variable when you already have targetPosition?

If you think a bit more about the 3 names I suggested and the idea behind them, you’ll certainly make this work.

Privacy & Terms