How can i make my gun have damage dropoff (it does more damage when close than when at a distance)
my game is in 2d and i use c#
How can i make my gun have damage dropoff (it does more damage when close than when at a distance)
my game is in 2d and i use c#
i figured out how to do it here’s the script:
private float distancePercent;
public LayerMask whatToHit;
private float distance;
private float clampedDistance;
public float tempMaxDamage= 100;
public float tempMinDamage= 10;
public float realDamage;
public float tempDamageDropStart =10;
public float tempDamageDropEnd = 100;
public void Start(){
Vector2 thispos = new Vector2 (transform.position.x, transform.position.y);
RaycastHit2D hit = Physics2D.Raycast (thispos,transform.right, 100, whatToHit);
Debug.DrawLine (thispos, transform.right*100, Color.cyan);
if (hit.collider != null) {
Debug.DrawLine (thispos, hit.point, Color.red);
distance = Vector2.Distance(thispos, hit.point);
clampedDistance = Mathf.Clamp(distance,tempDamageDropStart,tempDamageDropEnd) - tempDamageDropStart;
distancePercent = 100- clampedDistance* (100/(tempDamageDropEnd-tempDamageDropStart));
realDamage = tempMinDamage +(tempMaxDamage - tempMinDamage)* (distancePercent/ 100);
Debug.Log("We hit something and did"+realDamage+"damage");
}
i put it on my bullet prefab and it works