• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by VortexDEV · Feb 09, 2017 at 03:29 PM · rigidbodyfpsshootingdistancedamage

Damage drop-off over distance on a physical projectile

Hey!

I'm currently shooting a physical projectile in my weapon controller, that uses a sphere collider and a rigidbody, and has an initial velocity of e.g. 900m/s, and i'm wondering how I would go about adding a damage drop off to my bullet. Currently my bullet has a static base damage, and that gets altered to 0.5x, 1.0x, 2.5x depending on which part of the body it hits. I'm looking for a way to create a similar system to games such as Battlefield, where you have a minimum and maximum damage, and a drop off start and drop off end distance.


For example, let's say I have a weapon with these stats

Maximum Damage: 56

Minimum Damage: 39

Drop Off Start Distance: 36m

Drop Off End Distance: 65m


Basically, when the bullet fires, it has a damage of 56, but once it has travelled 36 metres, the damage starts progressively dropping from 56, until it travels 65 metres and drops to its lowest damage, 36. During this drop off distance, I want the damage of the bullet to progressively drop in a linear decline, rather than just setting it to the damage at that distance.

Does anyone know how I can achieve this?

Thanks, VortexDEV

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VortexDEV · Feb 10, 2017 at 04:00 AM 0
Share

Bump (due to approval time)

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by alankemp · Feb 10, 2017 at 02:05 PM

You can use Mathf.Lerp to interpolate between two values. In this case, you want to lerp between your min and max damage by the amount you are in the drop off range.

         float GetDamage(float bulletDistance, float minDamage, float maxDamage, float dropOffStart, float dropOffEnd)
         {
             // Use the min/max damage if we're outside the drop off range
             if (bulletDistance <= dropOffStart) return maxDamage;
             if (bulletDistance >= dropOffEnd) return minDamage;
 
             // Where in the drop off range is the distance we are interested in?
             // We want this in the range of 0 at the start to 1 at the end so we can use Lerp
             float dropOffRange = dropOffEnd - dropOffStart;
 
             float distanceNormalised = (bulletDistance - dropOffStart) / dropOffRange;
 
             return Lerp(maxDamage, minDamage, distanceNormalised);
         }


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VortexDEV · Feb 11, 2017 at 04:46 AM 0
Share

This seems to work quite well, but i'm having some problems actually deter$$anonymous$$ing how far the projectile has travelled. I've tried the basic Distance = Velocity * Time formula using the initial velocity of the projectile, and Time.time, but this doesn't seem to give accurate results. I'm currently calculating it by having an initial starttime variable that's set when the object is instantiated, and calculating the distance travelled on collision. It doesn't seem to work, sometimes it works, most of the time it doesn't, and it tends to give weird results, here's the code i'm using for it

 private float startTime;

 void Awake(){
    startTime = Time.time;
 }

     void OnCollisionEnter(Collision collision) {
           distanceTravelled = 400 * (Time.time - startTime);
     }

Any suggestions on accurately finding the distance the projectile has travelled?

Thanks!

avatar image alankemp · Feb 12, 2017 at 06:39 PM 1
Share

You know where the bullet started (where you spawned it), and you know where it hit.

You can use Vector3.Distance to work out the distance between two points.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Disable Damage Raycast? 1 Answer

Set up time between each instantiated object? 2 Answers

Dash ability on rigidbody FPS player acting strange. 0 Answers

I can't get my rigidbody reference set on my script in the editor? 2 Answers

How do i get an object to destroy another after multiple hits? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges