public void Instance_OnBallLaunched(float flightTime, float velocity, Vector3 initial, Vector3 target)
        {
            BallLaunched temp = OnBallLaunched;

            if (temp != null)
            {
                temp.Invoke(flightTime, velocity, initial, target);
            }
        }
Beispiel #2
0
        public void Launch(float power, Vector3 final)
        {
            //set the initial position
            Vector3 initial = Position;

            //find the direction vectors
            Vector3 toTarget   = final - initial;
            Vector3 toTargetXZ = toTarget;

            toTargetXZ.y = 0;

            //find the time to target
            float time = toTargetXZ.magnitude / power;

            // calculate starting speeds for xz and y. Physics forumulase deltaX = v0 * t + 1/2 * a * t * t
            // where a is "-gravity" but only on the y plane, and a is 0 in xz plane.
            // so xz = v0xz * t => v0xz = xz / t
            // and y = v0y * t - 1/2 * gravity * t * t => v0y * t = y + 1/2 * gravity * t * t => v0y = y / t + 1/2 * gravity * t
            toTargetXZ = toTargetXZ.normalized * toTargetXZ.magnitude / time;

            //set the y-velocity
            Vector3 velocity = toTargetXZ;

            velocity.y = toTarget.y / time + (0.5f * gravity * time);

            //return the velocity
            Rigidbody.velocity = velocity;

            //invoke the ball launched event
            BallLaunched temp = OnBallLaunched;

            if (temp != null)
            {
                temp.Invoke(time, power, initial, final);
            }
        }