Bullet trajectory drag problem

Hello you fine individuals,

I’m trying to make an object based ballistics systems (not raycast) and am having difficulty simulating drag. For some reason, when the bullet is fired in directions containing multiple horizontal axis’ such as(0.52,0.087,0.84), after some distance is travelled the bullet begins rearing off to the left or right, instead of just following its initial direction. Any help is appreciated, code below. The physics relating values such as the dragCoefficient and the force equations were found online. The startingDirection is set by another script when the bullets instatiated, and is given the value of the gun barrel’s transform.forward.

[Header("Movement")]
    public float initialSpeed = 822;
    [SerializeField] private float gravityForce = 9.81f;

    public Vector3 startingDirection;

    public Vector3 movementVelocity;
    private Vector3 lastPos;

    [Header("Drag")]
    [SerializeField] private float airDensity = 1.225f;
    [SerializeField] private float dragCoefficient = 0.295f;
    [SerializeField] private float refArea = 2.97025e-05f;

    private float dragConstValues;
    private float terminalVelocity;

    [SerializeField] private float mass;

private void Start()
    {
        movementVelocity = startingDirection * initialSpeed;

        dragConstValues = dragCoefficient * airDensity * refArea;
    }

private void Update()
    {
        BulletMovement();
    }

private void BulletMovement()
    {
        lastPos = transform.position;

        Vector3 newMovement = movementVelocity * Time.deltaTime;

        Vector3 velocity = (transform.position + newMovement - lastPos) / Time.deltaTime;

        Vector3 dragForce = dragConstValues * (Vector3.Scale(velocity, velocity) / 2);

        Vector3 newDrag = -dragForce / mass * Time.deltaTime;
        newDrag.y += -gravityForce * Time.deltaTime;

        movementVelocity += newDrag;

        transform.position += movementVelocity * Time.deltaTime;
    }

I would just place the bullets forward as it’s starting direction, then as you do set an initial speed, for when it’s first fired. Then as it’s flying have a value of current speed, which gets reduced by a drag value, probably compounding since you don’t want it to lose any sort of speed during it’s “forced” travel. But then drag getting higher in value once it’s passed that point, as well as gravity.

So drag would just be a current speed modification, as velocity would be forward * currentSpeed, and gravity could be a new directional adjustment to aim forward towards the ground, or just a positional adjustment to how far the objects position.y starts lowering. I would suggest direction, as it would move down on it’s own.

One system I heard of recently was a “Verlet” equation? that makes it’s own physics from positional values, so you might want to look into that. But if attacking physics on your own, you really have to watch data, frame by frame(bool set to false each frame, to click true for next frame logic).

And if I were to tackle this, I would have a force value, as gravity or drag wouldn’t activate until force drops below a threshold(of bullet lifetime). This way a bullet can be generalized, and it’s the technical gun that adds more or less force(caliper of gun/bullet). Plus you can also have said force value be what gives damage(if you’re going that technical).

Not sure what all those computations are… this is how I do basic ballistics:

    void Update ()
    {
        velocity += gravity * Time.deltaTime;

        velocity -= velocity * drag * Time.deltaTime;

        // use Rigidbody.MovePosition() if you are using physics!!!
        transform.position += velocity * Time.deltaTime;
    }

That’s it, drag and gravity and updating position over time.

Full script: makegeo/makegeo/Assets/Scripts/Ballistic3D.cs at master · kurtdekker/makegeo · GitHub

Thanks to both of yall, you helped clear things up and got my brain following! :):):slight_smile: