Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Projectile Arc Path

edited May 2020 in Programming

I want to add a mortar to my tower defense game. I need the projectiles of the mortar to go in an arc, but if I do that the enemies just dodge it. Any help?


  • edited May 2020

    I use C# btw.

  • Hi Fire_Knight9999,

    I am new to game dev but I would add a trial renderer component to the mortar "bullet" prefab. I used this tutorial to apply trial effect to my bullets in the game am currently working. Hope it helps.

  • Hi miguel8822,

    The game is 3D sooooo the tutorial does not work for me.

  • ABOABO Member

    Maybe this helps you:

  • Well this is not shooting something, but just showing the trajectory

  • ABOABO Member

    Well, then you should try this Unity project:

  • I will check it later thx.

  • That is a player shooting the bullets, I want it to be an automatic turret.

  • Adding trial renderer component will work in 3D as well. If you only want the trajectory to be shown, make the "bullet prefab" an empty object. Then make this empty object a prefab and add the trial renderer component. Here I found a guide showing how to add the trial renderer component to bullets in a 3d project.

  • That shows how to make a trail behind the bullet and NOT how I should make it move in an arc.

  • What do you mean by arc then

  • I meant that the video does not show any way of making a projectile move along its path.

  • MrSlugMrSlug Member

    I think what @Fire_Knight9999 is meaning is he wants to be able to fire a projectile along a parabola (An Arc), like firing a mortar or throwing a grenade.

    For this kind of projectile I'd point you to this Unity Answer:

    The user has listed a number of answers to this kind of question which should help you on your way.

    Hope this helps!

  • Are you sure it works on a moving target?

  • Enemies can change directions instantly

  • MrSlugMrSlug Member


    I've not tested these solutions personally, you'll need to code the targeting side of things

  • Well, he does not have any calculations for target velocity

  • MrSlugMrSlug Member

    The code provided shows how to get the projectile travelling in an arc.

    The rest you'll have to experiment with and tweak it to your liking.

  • My enemy is always moving, I can't seem to find the solution to it.

  • mhm depending on how you have set it up, your different enemy classes have different speeds? When the mortars aims at one enemy, you could therefore do some calculation so that it lands in front of him when it lands. Other than that, you could also just let it shoot at the original position, as many tower defense games do and increase the damage radius

  • @joachim747

    Yes, my enemies have different speeds. I can't simply do "calculation" because I have different maps and therefore I have different paths which means it is practically impossible to do that.

  • Well, that depends on how your logic is working. Do you have e.g. a field setup, through which the enemies move? Then you can pretty much do some sort of advance calculation, as the enemy is moving through a field by x.

    if you have no such system in place, it is really more of a prototyping thing. You will have to get the enemy position quite frequently to update the position of the projectile. Depending on how far the projectile is from the enemy position (raycasts) you should apply a bit force to it or reduce it. From here on it is pretty much tweaking the values so that it looks smooth for your levels...

  • I am doing a 3D environment joachim747 so the z axis too.

  • edited May 2020

    @joachim747 can you give me an example script that works for this?

  • MrSlugMrSlug Member


    You originally asked how to make a mortar projectile travel in an arc, you've had some resources provided for that.

    This is not the place to have a script written for you. Do you have any working code for us to review? If so, please post it and we can try and help you with it.

  • No, I do not have any code yet. What I've thought of is to launch the ball at the Start method. Then on update use Vector3.Lerp to move the object towards the enemy. But that acts weird due to moving using both Vector3.Lerp and gravity.

  • joachim747joachim747 Member
    edited May 2020


    I am doing a 3D environment joachim747 so the z axis too.

    ... and? My outlined methods could work in 3d nervertheless ..

  • AquaXVAquaXV Administrator

    Hey there @Fire_Knight9999! We encourage all members to give a try at solving issues, I see amazing suggestions given to get started with this.

    If you need help with something specific in your scripts please add some code so it easier to help you! We don't encourage spoon-feeding code because then you won't learn anything.

  • @AquaXV you are right. I am working on a system right now for this. I will post it as soon as possible

  • edited May 2020

    I am having a problem here

    RecalculationData WaypointPercent(List<Vector3> waypoints, int currentWaypoint, bool shouldRecalculate)

    		if (shouldRecalculate)
    			waypoints = DrawPath();
    		Vector3 oldPos = startPos;
    		foreach(Vector3 point in waypoints)
    			Debug.DrawLine(oldPos, point,;
    			oldPos = point;
    		Vector3 currentPos = new Vector3(waypoints[currentWaypoint].x, startPos.y, waypoints[currentWaypoint].z);
    		float percent = Mathf.Abs(Vector3.Distance(currentPos, waypoints[waypoints.Count - 1]) / Vector3.Distance(waypoints[0], waypoints[waypoints.Count - 1]));
    		percent = 1 - percent;
    		Vector3 nearestPoint = startPos;
    		float nearestDist = Vector3.Distance(nearestPoint, new Vector3(waypoints[currentWaypoint].x, startPos.y, waypoints[currentWaypoint].z)); ;
    		for (int i = 0; i < waypoints.Count; i++)
    			Vector3 pos = new Vector3(waypoints[i].x, startPos.y, waypoints[i].z);
    			float posDist = Vector3.Distance(pos, currentPos);
    			if (posDist < nearestDist)
    				nearestPoint = pos;
    				nearestDist = posDist;
    		int waypoint = 0;
    		for(int i = 0; i < waypoints.Count; i++)
    			if (waypoints[i] == nearestPoint)
    				waypoint = i;
    		if (waypoint == currentWaypoint)
    		return new RecalculationData(waypoints, waypoint);

    RecalculationData is a struct containing a Vector3 and an int.

    The waypoint int is one while the currentWaypoint is one, else it is always zero for some odd reason.

    startPos is the transform.position of the bullet at the start of the game.

Sign In or Register to comment.