How to fire in front of enemy

I’d like to fire in front of my enemy, but I can’t seem to figure out how I can manage to do that.

Here is my method:

private void FireInFrontOf(Transform target)
{
    // Set height to fire from
    Vector3 position = HeadTransform.transform.position + new Vector3(0, FireHeight, 0);
    
    // Instantiate Projectile
    GameObject projectileGO = (GameObject)Instantiate(ProjectilePrefab, position, HeadTransform.transform.rotation);

    // Retrieve Projectile
    Projectile p = GetProjectile(projectileGO);
    if (p == null) return;

    // Set Projectile attributes
    p.Target = target;
    p.Damage = Damage;
    p.Radius = Radius;
}

I already found this:

target.forward*Time.deltaTime*DistanceUpFront;

But if I apply that to the targets position parameter before setting the projectile properties, the target moves over my screen.

Can anyone help me with this problem? Thank you!

In the attached files is a quick sketch for clarification of what I want to achieve.

2866361--209916--FireInFrontOf.png

In the code you posted, you wouldn’t apply that position to the target, the target will continue moving normally. That calculation has some issues but it looks like somebody is trying to use it to predict where the target would be.

You need to know the gun position, the target position and heading, the projectile speed, and the target speed. You’re looking for the “intercept” – the point at which your projectile’s line intersects the target’s line. This is called “leading” the target and the Interwebs are full of solutions. Here’s one:

http://wiki.unity3d.com/index.php?title=Calculating_Lead_For_Projectiles