0

I'm digging in unity tutorial "3DGameKit". when a game monster shot a grenade, call the function GetVelocity() to set rigid body's velocity. In order to figure out this function,I studied Bezier curves and parabolic motion, I think it is more like parabolic motion,I know that in order to improve performance, the square may be directly used to participate in the calculation,but the relevant code does not seem to be related to the formula. I am sure I should be missing something important,the code is below, and the variable “projectileSpeed” is a float:

 private Vector3 GetVelocity(Vector3 target)
        {
            Vector3 velocity = Vector3.zero;
            Vector3 toTarget = target - transform.position;

            // Set up the terms we need to solve the quadratic equations.
            float gSquared = Physics.gravity.sqrMagnitude;
            float b = projectileSpeed * projectileSpeed + Vector3.Dot(toTarget, Physics.gravity);
            float discriminant = b * b - gSquared * toTarget.sqrMagnitude;

            // Check whether the target is reachable at max speed or less.
            if (discriminant < 0)
            {
                // Debug.Log("Can't reach");

                velocity = toTarget;
                velocity.y = 0;
                velocity.Normalize();
                velocity.y = 0.7f;

                Debug.DrawRay(transform.position, velocity * 3.0f, Color.blue);

                velocity *= projectileSpeed;
                return velocity;
            }

            float discRoot = Mathf.Sqrt(discriminant);

            // Highest shot with the given max speed:
            float T_max = Mathf.Sqrt((b + discRoot) * 2f / gSquared);

            // Most direct shot with the given max speed:
            float T_min = Mathf.Sqrt((b - discRoot) * 2f / gSquared);

            // Lowest-speed arc available:
            float T_lowEnergy = Mathf.Sqrt(Mathf.Sqrt(toTarget.sqrMagnitude * 4f / gSquared));

            float T = 0;
            // choose T_max, T_min, or some T in-between like T_lowEnergy
            switch (shotType)
            {
                case ShotType.HIGHEST_SHOT:
                    T = T_max;
                    break;
                case ShotType.LOWEST_SPEED:
                    T = T_lowEnergy;
                    break;
                case ShotType.MOST_DIRECT:
                    T = T_min;
                    break;
                default:
                    break;
            }

            // Convert from time-to-hit to a launch velocity:
            velocity = toTarget / T - Physics.gravity * T / 2f;

            return velocity;
        }

it works fine in my demo. I will very appreciate if somebody can explain that.

  • 1
    As long as the game's physics are realistic, thrown objects will follow a parabola. Please be more specific in your question though. What part of the code does not seem relevant to what formula. – Verpous Jun 11 '23 at 12:36
  • The formula I refer to is here:[range of projectile](https://en.wikipedia.org/wiki/Range_of_a_projectile), and the time to hit is :t = v*sin/g + sqrt(v^2 + sin^2)/g, and I don't know variable "discriminant" represent which part, and why "discriminant" can Calculate reachable. @ Verpous. the monster and player has a height offset, which means y0 is positive – zhang0xf Jun 11 '23 at 13:32
  • there is a write error about t, it should be t = v* sin / g + sqrt(v^2 * sin ^2 + 2* g* y0)/g, and you find it from the web more easy.@ Verpous – zhang0xf Jun 11 '23 at 13:51
  • The [discriminant](https://en.wikipedia.org/wiki/Discriminant) is the part of the quadratic formula that's inside the square root. This is not a function that computes the time to hit of some parabola, it's a function whose job is to find a parabola which will touch the target. And there could be many so part of its job is to pick one. You can find explanations for how to do that online, here's one for example: https://tasks.illustrativemathematics.org/content-standards/tasks/379 – Verpous Jun 11 '23 at 14:34
  • thanks for pointing me in the right direction, and for the people with the same question, you can also visit here:[How do I determine a good path for 2D artillery projectiles?](https://gamedev.stackexchange.com/questions/71392/how-do-i-determine-a-good-path-for-2d-artillery-projectiles/71440#71440), the key to the algorithm is parametrizing parabolas family on the duration of projectile's flight. – zhang0xf Jun 12 '23 at 09:12

0 Answers0