How to get a Projectile to follow a Parabola

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

public class MissilePathing : MonoBehaviour
{

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


    void Start()
    {
        startingPosition = transform.position;
        waypointsMissile = FindObjectOfType<Player>().GetWaypoints();
    }

    void Update()
    {

        Move();
    }


    private void Move()
    {//qui è dove una volta nato conlo faccio muovere 
        if (waypointIndex <= waypointsMissile.Count - 1)
        {

            var targetPosition = waypointsMissile[waypointIndex].transform.position + 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 

    }

}

So this is the current right code, something changed, now when i move and shoot the trajectory is somehow linked on where i am but it is still slightly wrong, like now it’s like every waypoint is moved down/right from the player
at least it is linked to where i am now
image

Maybe the path is “wrong”. In the scene view, place a shooter. Then set the path parent position to the position of the shooter. If the waypoints are off, move the waypoints to the desired position.

look i found out what is going on, i found out that the pathMissile gameobject which contains the waypoints when placed on unity is like this

now if i place the pathmissile container on the center of the player i get this
image
WHICH IS THE EXACT behavior of the trajectory i get
so i tried to change the position of the waypoint with the pathMissile standing in the center of the player and it all worked, but WHY? i don’t like this how can i avoid this behavior?

why is it affected by the pathmissile position :thinking: what if i have a bigger sprite and i don’t know exactly where the center of the player is, i don’t like to put the path missile “manually” at the center and then manipulate the waypoints, i would like a more dinamic approach

i think it’s because it is like we are on coordinates of a graph but mh, correct me if i am wrong but the other time i used move Toward this didn’t happen because i got the Object to spawn exactly on the first waypoint while here we have the projectiles moving from my player right? it is a different usage of em so i don’t think i can make comparison to the spawning and pathing of the enemies and boss

when i say cointainer i refer to this
image

maybe this happens due to the localPosition thing right? :thinking:

Your algorithm works as I imagined it. The projectile is supposed to start at the startingPosition, then it should move toward the first waypoint. That’s exactly what your screenshots show. And now move the waypoints up and leave the parent game object where it is.

If you find the idea difficult to grasp: Do you know those parabola stencils? You take that thing and move it around to draw parabolas. You can reuse it in different places. The parabola always looks the same, no matter where you draw it or whether you rotate the stencil.

You do the same here. Your path game object is the “parabola stencil”. You reuse it at multiple positions.

if i leave the container of the waypoints at the 0,0,0 coordinates it makes the shooting behave not with the path i want, to have the trajectory i imagine i have to put the PathMissile on the center of the player

image

The position of the parent is completely irrelevant. Only the position of the children relative to the parent matter.

so i have to manually put the container where i first instantiate the player object? what if i let’s say move by mistake the player object, it will mess up with everything

Yeah but did you understand what is going on now ? the path is dependent on where the Container of the waypoints, the prefab is instantiated in unity
or likely it is dependant on where the center of the player is, so to get it right i have to put the center of the container to match with the center of the player and then manipulate the waypoints manually

image
maybe i’ll make it a child of player so when i reset it , it gets to the center, but again does this happen because we set it as LocalPosition?

You need some reference point (pivot point). Otherwise, the children would not have a position. Everything is relative to something else. That’s how “the world” works. Maybe you’ve heard about that guy named Albert Einstein. :wink:

You are creating a path template here. And since there is no such thing as a coordinate without a reference point, you need a root object. Yours is “Path Missile”.

However, we are only interested in the relative position of the children because we want to move the path around just like the parabola stencil. Relative to the pivot point, but not to a specific pivot point with a predefined position.

In your screenshot, the relative position of the first waypoint is below the pivot point. It does not matter if you move the “Path Missile” left or right, up or down. The first waypoint will always be below the pivot unless you move the waypoint up.

1 Like

ok i got it all, with clarity

–i managed to get the behavior i wanted, now only two things remains haha one is a problem i figured out only now by changing the sprite and it is the rotation of the object , which i would like to face toward the waypoints, so up, i know i could just change the rotation of the image , but i was wondering if i could dinamically change the rotation for each waypoint, it would be really cool and super useful in future projects too.

–Last thing is that now i need to understand how to get the damage go to a “radious” like an exploding bomb and not damaging only the enemy who has been hit, but also who surround him :thinking: :thinking:

–(lastly a little curiosity by me, is there a way to get like in automatic the waypoint for a super precise parabola? like implementing in some way the mathematic formulas for it? i know this is a bit more complicated subject so you can also not answer this)

Awesome! Your video looks really great. Maybe you could add two more waypoints to smooth the parabola a bit but apart from that, it looks fantastic. And the good thing is that you have a flexible solution if you wanted to add other path shapes. :slight_smile:

For future reference, just type your idea into your favourite search engine. It is very likely that people in the path had the same idea as you and developed a solution for “your” problem. :wink:

Maybe scaling the transform with a coroutine? Or an animation?

Sure. Create a parabola function to generate your waypoints, then rotate the imaginary path object with a rotation matrix. Alternatively, you could put everything into one line and generate the path while the projectile is moving, but I would rather opt for the “path generator” because at some point, the player will not notice anymore whether there are 100 waypoints on the path or 1000.

This is how an example output could look like:

Vector3[] path = new Vector3[] {
   new Vector3(0f, 0f, 0f),
   new Vector3(1f, 1f, 0f),
   new Vector3(2f, 2f, 0f)
};

Thank you kindly!
Mhh so how could i implement the parabola function :thinking: maybe i could take from the waypoint list only the starting point and the “landing” point, and calculate the point in between with a formula, but how could that take place in my code? :thinking:

For the explosion damage i found this video GRENADE / BOMB in Unity (Tutorial) - YouTube
do you think this could help?

You will have to develop an algorithm to solve this problem. The quadratic function has got different parameters. You need three things to calculate the path: startingPoint, endPoint and vertex.

If you haven’t seen this before, watch a few videos on Youtube, or, if you want to dive deeper into maths, enrol in Ben’s and Gary’s maths course.

You are in the Laser Defender section, which means that you already have some experiencing in game developing. If that video looks promising to you, give it a go. Be a game developer! :wink:

1 Like

Yes thank you for all, i think i’ll give some shoot to all of this and then i’ll proceed with the next part of the 2d course! maybe i’ll stick for a bit with the pathing solution i got before, because even if it is not super elegant and precise, it’s still functional and i can get with it a lot of different paths!
:smiley:

Ok i know i said i would have tried this alone but i need a last final little hand, i tried to use the lookAt method but i didn’t manage to rotate the object only in the Z-axis, it kept changin all the object axis going outside the 2d field, i looked over the internet and i saw that for the anchoring of only 1 or 2 axis it is adviced to use a formula like this:

  private void Rotate(Vector3 targetPosition)
    {
        Vector3 difference = targetPosition - transform.position;
        float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0, 0, rotationZ);

    }

it’s a math based formula and i understand it, it works but the problem is that unlike the lookAt method the movement is a little sloppy, how could i do an interpolation between the rotation in each point? i saw people using methods like Larp but it didn’t work


this is what happens using lookat

i tried to also give as parameters of the lookat a vector 3 with only the z rotation but it didn’t worked

just passing by to say that i solved this haahha i even implemented a lot of different new scripts, powerups, healt pick ups , animation and i managed to get some sort of loot system , (i simply put a random number generator inside every enemy and when i get to some range at runtime it set the enemy to drop a certain item, it works pretty well and i’m surprised because i thought of this all by myself ahah)
this is how i changed the code

 private void Rotate(Vector3 targetPosition)
    {
        Vector3 difference = targetPosition - transform.position;
        float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
       var quat= Quaternion.Euler(0f, 0f, rotationZ);
        transform.rotation = Quaternion.Lerp(transform.rotation, quat, Time.deltaTime * 5);
    } 
    

Great job! The video looks really fantastic. Did you use the path or did you write a script for the parabolic movement?


See also:

1 Like

Thank you! no it’s still the waypoint path

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms