Answer the question
In order to leave comments, you need to log in
How to teach bots to take into account the gravity of the planet when shooting?
The bots shoot quite accurately in empty space, but they constantly miss if gravity starts to affect the shooting. The gravity
script is taken from here .
Answer the question
In order to leave comments, you need to log in
As for the script, it's strange:
1) It only works for objects that are involved in collisions.
2) It does not take into account the distance to the central body, only its mass, and works only for cases where the distance between opponents is thousands of times less than the distance to the central body, i.e. more or less accurate somewhere in the outskirts of the star system, away from the star. At closer distances, it begins to shamelessly lie.
3) It fills an array with objects, which the script does not really need to work, which is not cleared anywhere. This is a memory leak.
4) It can be applied only to one body - the central one. Otherwise, it multiplies the memory leak and consumes a lot of the processor. In some cases, it can be replaced with a trigger . And it's often optimizedcalling every few dozen frames , not every frame. They also write that specifying a layer mask in Physics.OverlapSphere improves work efficiency.
About the leak.
I don’t know the intricacies of unity, I can’t say for sure how serious it is. But two options are possible:
1) the script object is recreated every frame, and from time to time obsolete instances are collected by the garbage collector.
Then the leak is equal to the size of the array multiplied by the number of frames per second and the timeout of the garbage collector.
Provided that the script is used only on one object (I hope you didn’t think of applying it to all objects?), for 1000 objects in the scene, 60fps and a 10 second garbage collector timeout, the leak will be 5..50Mb - this is how much memory the game will eat wasted, not used anywhere, only for one instance of this script.
If the script is applied to 10 objects, the leak will increase to 50..500Mb.
And if there are 1000 objects?
This is how games are born that require 16GB of RAM.
This is not to mention the useless waste of processor resources: if you foolishly apply the script to all objects, it will work effectively only on one, but it will eat up memory and the processor will be for everyone.
On 1000 objects, the processor consumption of this script will increase by 1000000 times: 1000 scripts will each have to process 1000 objects.
This is how games that require top-end hardware are born.
Only 2 easy errors with one script (far from the main one) - and such potential! :)
2) One instance of the script is used, it is not recreated every call.
Then the volume of the array is multiplied by fps every second until the array fills up all the memory.
And the garbage collector will not help here, because. the script exists as long as the main object exists, i.e. while the level is loaded - the entire game session.
For the same conditions, the leak in the first second will be 0.5..5Mb, and every second it will increase by the same amount. For an hour of play, the leak will be from 2 to 20 GB, depending on the size of the structures.
The CPU leak will remain the same as in the first option.
Your option is the first one, otherwise the script would have worked exactly once, and gravity would disappear after the first frame: the array is used to check bodies to which gravity has not yet been applied.
And this is not so - since there is a constant error, gravity works longer than one frame.
By the way, this check is superfluous, it is not in the documentation .
Regarding aiming errors: correct for gravity when aiming.
It’s not a fact that this will help you, after all, your script is crooked, and it’s not clear whether you are using it correctly - what are the maximum distances between opponents, what are the minimum distances from opponents to the central body, what is the size of the central body, how are shells created (independent or tied to the one who shot), how their speed is set (constant or relative to the speed of the shooter), what is the speed of the projectile and the target.
But the simplest one is:
dy=g*t*t/2
t=l/v
dy - projectile displacement by gravity at the current distance
g - gravity value, local (near the shot point). In your script, gravity is constant, does not depend on coordinates, and is equal to the mass of the central body, so instead of g, you can substitute the mass of the central body.
t - projectile flight time
l - direct distance to the enemy.
v is the global velocity of the projectile (relative to the world).
Restrictions:
Local gravity is not applicable if the distance is comparable to the distance to the central body - an additional allowance for the curvature of the gravitational field is needed there. Gravity at different points of the projectile path will be different, and this will greatly reduce accuracy, especially at long distances - by many orders of magnitude.
The direct distance is inapplicable if the distance is comparable to the distance to the central body - an additional consideration of the curvature of the gravitational field is needed there. The distance will not be straight, but an arc, which means the projectile will travel more distance, it will fly longer, and more correction is needed.
Если время полета больше нескольких секунд, придется учитывать влияние гравитации на скорость снаряда. Снаряд будет ускоряться или замедляться гравитацией, а значит точность с дистанцией начнет быстро падать.
Если скорость цели сравнима со скоростью снаряда - придется учитывать, что цель движется. Пока снаряд летит в точку прицеливания, быстрая цель оттуда уже убежит, и точность никакой не будет.
Формулу прицела меняете с пересечения двух прямых, на пересечение прямой и параболы.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question