how to calculate speed with in range value.
I made a projectile script and it works just fine but it needs some upgrades like calculating range using speed and gravity or range with speed and gravity just to make everting more clear than now script is using rigid body to add force to the projectile finally sorry for bad english.
Answer by tylerdtrudeau · Mar 05, 2021 at 09:15 PM
Im not sure what you are trying to achieve. Do you want the projectile to have a constant speed? keep accelerating? slow down due to drag? Do you want the projectile to slowly drop due to gravity like a bullet would shot over long distances? theres alot of info needed about what youre trying to do before anyone can really help you achieve it.
I already did all stuff above but i have speed var isnted of range so what i want to do is get auto speed when set range value with my rigidbody so for example when i set range to 100m it sets speed to fit this range or just calculate range value from speed hope u got my meaning
Sorry but those sound like 2 different things. Set speed and find range is constant speed with variable time depending on range. Calculate range based off of speed would be constant time with variable range depending on speed.
lets try some examples to get some clarification.
eg 1 - If you have a set range (say 100m) and you want to set the speed to reach that range (for ease of calculation lets say 10m/s so it takes 10 seconds to reach the target) then if you change the target to 50m away it will only take 5 seconds to get there as the speed is 10 m/s. so this is constant speed but variable time depending on range.
eg 2 - You have a flight time of 10 sec before projectile hits the ground/ stops. If you set the speed to 12 m/s then with simple multiplication you can deter$$anonymous$$e the range to be 120m. if you increase the speed then the range increases and vise versa. So thats a super simplified way of finding range with time and speed.
eg 3 - Now if you want to go super realistic and factor all the variables in that modern FPS games do and update velocity dynamically based off range it gets alot more complicated. You need to factor in gravity (9.81 m/s^2) to deter$$anonymous$$e how quickly the bullet is being pulled downwards aka its flight time, if the bullet is fired straight horizontal its super easy, if its fired slightly upwards (like a snipers long shot) then you need to do some trigonometry to get the vertical component of the velocity and use gravity to establish flight time. You will need to calculate in drag of the projectile to deter$$anonymous$$e how fast its slowing down and therefore its velocity is dynamic throughout the flight. Finally once you have an equation to figure out all this stuff then you can simply plug in the range and it will tell you the velocity that the bullet needs to be fired at to reach the target. If you keep everything as horizontal shots then the time to reach the target should always be same as a bullet falls vertically at the same speed regardless of horizontal velocity (whether you drop it from 2m high or fire it from a gun 2m high that is pointing straight sideways) (lets ignore curvature of the earth for longer shots to make it easier)
if eg 3 is what youre looking for I would start by leaving gravity on but reducing all drag to 0 as a start point. once you have that working properly then intoduce drag back into it and adjust you calculations to reflect that. Just start simple then add one layer of complexity at a time and test/ debug every step to make sure youre getting the desired results.
Thx bro this is little bit complex but i will try it thx.
Your answer
Follow this Question
Related Questions
Issue with shooting projectiles in the direction of the arrow keys 1 Answer
Unity glitches using Ridigbodies as bullets in multiplayer- am I missing something? 0 Answers
How would one go about implementing Unreal's Suggest Projectile Velocity function in Unity? 1 Answer
Is there any good sample code for shooting a projectile and collision detection? 0 Answers
Projectile throw in 3rd person 0 Answers