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;
}