Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Range based damage (raycast shooting)

Discussion in 'Scripting' started by LambdaTheDev, Feb 22, 2021.

  1. LambdaTheDev

    LambdaTheDev

    Joined:
    Jan 15, 2020
    Posts:
    53
    Hey guys, I have a small problem.

    I am making a weapons system, and I don't know how to do a damage calculation.
    This is my current plan:
    - Player shoots, raycast starts
    - Target entity is captured.
    - I have a distance / maxdistance value (% of max distance)
    - And 2 variables: minDamage and maxDamage.

    And I have no idea how to calculate the damage.

    Anyone has an idea how to do it?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    We'd need more information because the calculation might not be as obvious to us as you think. Something like:

    Code (csharp):
    1. float damage = Mathf.Lerp(minDamage, maxDamage, distance / maxDistance);
    will smoothly move between dealing minimum damage at 0 distance to maximum damage at maximum range, but maybe you want a sweet spot or something where half the max range is optimal and it falls off along either side of the curve. You can use an animation curve to define that. I have a tutorial here: https://grozzler.tumblr.com/post/166310091676/animation-curve-tutorial -- don't be afraid of the name, you can use the curves for a lot more than just animation.
     
  3. LambdaTheDev

    LambdaTheDev

    Joined:
    Jan 15, 2020
    Posts:
    53
    It's exactly what I wanted. Thanks!