The best model for this if you aren’t using physics and are neglecting drag from the air is to give an initial speed to the projectile, and simulate the x movement with a constant speed and simulate the y movement with speed increasing downward in a first-order relationship with time (linear decrease) for gravity. I think we would need to change to this model before being able to move forward.
The simplest initial conditions would be a speed and an angle. You could easily derive the x- and y-speeds from this, which you would use sort of like this (sorry for any errors):
public float speed = 15f; //Forward speed of the projectile in units/s
public float angle = 60f; //Angle above the horizontal that the projectile will be fired in degrees
private Vector2 vel; //Velocity in component form. Speed and angle are inspector variables because they're easier to conceptualize
void Start()
{
vel = new Vector2(speed * Mathf.Cos(angle), speed * MathF.Sin(angle)); //Calculating the x- any y-speed based on speed and angle
}
void Update()
{
Vector3 tempPos = new Vector3();
tempPos.x = transform.position.x + vel.x * Time.deltaTime; //Move in x direction...
tempPos.y = transform.position.y + vel.y * Time.deltaTime; //...and move in y
vel.y -= Physics2D.gravity.y * Time.deltaTime; //Change y-speed due to gravity
transform.position = tempPos; //Apply movement
}
This should get you a reasonable looking arc. However, it leaves you guessing at what values to use for speed and angle to hit specific points.
Before going into more detail, I need to address your questions. It’s hard to tell whether you want to be able to check those values for a given arc, or whether you want to specify them. I would guess that you want to specify, but your own answer to Q1 is just a calculation, which is fine. The problem is that some of these initial condition specs are incompatible.
For example, you can’t specify the target position, angle, and flight time all at once. If you specify any two, the third value must be calculated, and has only two solutions at best, usually only one.
Another thing I want to point out: Average speed of the projectile to its target (Q4) and the time it takes to get there (Q5) are directly related and fully dependent. If you have one, you have the other, and you definitely can’t define them individually.
To try to answer the questions:
Q1: Looks good.
Q2: Is this the maximum height of the projectile in its arc? This will depend on the other values. If you specify the height, you will then only be able to specify one more value: initial speed, firing angle, or final position. For example, if the mortar sits at y = 0, and you specify a height of y = 10, and an angle of 30 degrees, there is only 1 initial speed that makes this possible, which also constrains the final position.
Q3: Just set the angle of the object to angle in the projectile script.
Q4/Q5: Specifying flight time is not straightforward, unless the terrain is completely flat (is it flat?). It would be easy to calculate a flight time based on other conditions, but saying “I want the projectile to be in the air for THIS long” is harder. Let me know what you’re going for here.
I think a good way to do this would be to allow two values to be selected as specified in the inspector, while the other two are calculated. This would make for a pretty flexible system. I can go on about how you might accomplish this, but first let me know if this is the sort of thing you’re going for, and whether I have the right idea about what you’re doing.