Пример #1
0
    //Display the trajectory path with a line renderer
    void DrawTrajectoryPath()
    {
        //How long did it take to hit the target?
        float timeToHitTarget = CalculateTimeToHitTarget();

        //How many segments we will have
        int maxIndex = Mathf.RoundToInt(timeToHitTarget / h);

        lineRenderer.SetVertexCount(maxIndex);

        //Start values
        Vector3 currentVelocity = gunObj.transform.forward * bulletSpeed;
        Vector3 currentPosition = gunObj.transform.position;

        Vector3 newPosition = Vector3.zero;
        Vector3 newVelocity = Vector3.zero;

        //Build the trajectory line
        for (int index = 0; index < maxIndex; index++)
        {
            lineRenderer.SetPosition(index, currentPosition);

            //Calculate the new position of the bullet
            TutorialBallistics.CurrentIntegrationMethod(h, currentPosition, currentVelocity, out newPosition, out newVelocity);

            currentPosition = newPosition;
            currentVelocity = newVelocity;
        }
    }
Пример #2
0
    //How long did it take to reach the target (splash in artillery terms)?
    public float CalculateTimeToHitTarget()
    {
        //Init values
        Vector3 currentVelocity = gunObj.transform.forward * bulletSpeed;
        Vector3 currentPosition = gunObj.transform.position;

        Vector3 newPosition = Vector3.zero;
        Vector3 newVelocity = Vector3.zero;

        //The total time it will take before we hit the target
        float time = 0f;

        //Limit to 30 seconds to avoid infinite loop if we never reach the target
        for (time = 0f; time < 30f; time += h)
        {
            TutorialBallistics.CurrentIntegrationMethod(h, currentPosition, currentVelocity, out newPosition, out newVelocity);

            //If we are moving downwards and are below the target, then we have hit
            if (newPosition.y < currentPosition.y && newPosition.y < targetObj.position.y)
            {
                //Add 2 times to make sure we end up below the target when we display the path
                time += h * 2f;

                break;
            }

            currentPosition = newPosition;
            currentVelocity = newVelocity;
        }
        return(time);
    }
Пример #3
0
    void MoveBullet()
    {
        //Use an integration method to calculate the new position of the bullet
        float h = Time.fixedDeltaTime;

        TutorialBallistics.CurrentIntegrationMethod(h, currentPosition, currentVelocity, out newPosition, out newVelocity);

        //First we need these coordinates to check if we have hit something
        CheckHit();

        currentPosition = newPosition;
        currentVelocity = newVelocity;

        //Add the new position to the bullet
        transform.position = currentPosition;
    }