Question by
andreyazbyn · Sep 20, 2015 at 06:57 PM ·
c#gundamagesidescrollerdrop
Damage Dropoff
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#
Comment
Answer by andreyazbyn · Sep 21, 2015 at 03:31 PM
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
Your answer
Follow this Question
Related Questions
Enemy wont deal damage 0 Answers
Is there drop event in slider? 0 Answers
Gun shoot problem? 1 Answer
3D Side Scroller Gun PickUp 0 Answers