Trajectory of a projectile

I’ve been trying to work out an idea for a ball to shoot from a motor to a target position

I can sort of get this to work with the following code, it does move a gameobject “cube so far” from one position to the other in a arc but it’s not realistic or close to it.

public Transform StartT;
  public Transform EndT;
  public float Height = 3F;
  public float Speed = 5F;

  private float mTime;

  void Update()
  {
   mTime += 0.04f;
   Vector3 currentPos = Vector3.Lerp(StartT.position, EndT.position, mTime);
   currentPos.y += Height * Mathf.Sin(Mathf.Clamp01(mTime) * Mathf.PI);
   transform.position = currentPos;
  }

Now here is my Motor hierarchical tree made with standard 3D objects for now.

-Mortor
–Base
—Dome
----Barrel
-----Centre (Empty GameObject used for rotation etc)
-----back
-----Projectile (The anchor point of the projectile when it’s instantiated)
-----Muzzle (Particle System)

There is some things I need to work out

Q1) - Distance (from start to end) got that
A1) - Length = Vector3.Distance(StartT.position, EndT.position);

Q2 - How height I want the projecttile
A2 - this will be I height I choose - the offset of the motor barrel height?

Q3 - The angle I must set the “Centre Gameobject” to make it look realistic enough.
A3 - I know I can calculate angle from a direction using Atan2.

Q4 - Speed (how fast it must travel from 1 to B)

Q5) - The time I want it to take.

lastly I’m no mathematician and I’ve seen some on wikipedia, but I just dont get it.

Thanks for any help.

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.

All your idea’s a really neat, as a bit of a play around I tried to implement the trajectory of a projectile (still not sure if i’m using it correctly) on wikipedia, with some sort of limited success, I’ve uploaded the little project on my dropbox it uses unity 5.5.2.3p3. but it will work on 5.6 its just does not convert back down to 5.5, as the material colours disappear.

Called CAndC.zip

https://www.dropbox.com/sh/wkwmfu6l30vhwiv/AABF_zapft0LhfpODWzkvkD-a?dl=0

I got turrets and Laser Cannon work fine, but mortar not so.

There is several problems with it as you will see(The first version I have disabled and commented out the Update method), did hit the target( if it did not move from the running position, as tracking was all wrong ish) I’ve been racking my little brain for several days on this, but enjoying the challenge.

I suppose it would be the velocity/and range(I could determine) and calculate the angle? which I think I’m doing with Angle 0 required to hit coordinate.

Thanks for the help