public void TestFindInitialVelocity4()
        {
            Tuple <Vector2, Vector2> solutions;

            solutions = ProjectileMath.FindInitialVelocity(2f, new Vector2(10f, 10f), -9.8f);
            Assert.IsNull(solutions);
        }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        Vector3 aimDir;
        float   timeToTarget;
        Vector3 targetVelocity = m_TargetStartTransform.forward * m_TargetSpeed;

        ProjectileMath.CalculateProjectileAim(m_ProjectileStartTransform.position, m_ProjectileSpeed, m_TargetStartTransform.position, targetVelocity, m_SolverIterations, out timeToTarget, out aimDir);

        Vector3[] projectilePositions = new Vector3[2] {
            m_ProjectileStartTransform.position, m_ProjectileStartTransform.position + (aimDir * timeToTarget * m_ProjectileSpeed)
        };
        m_ProjectileLineRenderer.SetPositions(projectilePositions);

        Vector3[] targetPositions = new Vector3[2] {
            m_TargetStartTransform.position, m_TargetStartTransform.position + (targetVelocity * timeToTarget)
        };
        m_TargetLineRenderer.SetPositions(targetPositions);

        m_ProjectileStartTransform.rotation = Quaternion.LookRotation(aimDir.normalized);

        m_ProjectileVelocityCalculated = m_ProjectileStartTransform.forward * m_ProjectileSpeed;
        m_TargetVelocityCalculated     = targetVelocity;
        m_TimeToTargetCalculated       = timeToTarget;


        UpdateSimulation();
    }
Ejemplo n.º 3
0
    public void SetTargetWithAngle(Vector3 point, float angle)
    {
        currentRadian = angle * Mathf.Deg2Rad;

        targetPoint = point;
        //GizmosHelper.DrawBox(point, Vector3.one * 0.2f, Color.yellow);
        Vector3 direction = point - firePoint.position;

        //if (Vector3.Angle(direction, transform.forward) > 10)
        //{
        //    // Haven't face the right direction.
        //    projectileArc.gameObject.SetActive(false);
        //    return;
        //}
        //else
        //{
        //    projectileArc.gameObject.SetActive(true);
        //}

        float yOffset = direction.y;

        direction = Math3d.ProjectVectorOnPlane(Vector3.up, direction);
        float distance = direction.magnitude;

        currentSpeed = ProjectileMath.CalculateLaunchSpeed(distance, yOffset, Physics.gravity.magnitude, angle * Mathf.Deg2Rad);

        projectileArc.UpdateArc(currentSpeed, distance, Physics.gravity.magnitude, currentRadian, direction, true);
        SetThrowPoint(direction, currentRadian * Mathf.Rad2Deg);
    }
Ejemplo n.º 4
0
    public float GetRequiredSpeed(Vector3 point, float angle)
    {
        Vector3 direction = point - firePoint.position;

        float yOffset = direction.y;

        direction = Math3d.ProjectVectorOnPlane(Vector3.up, direction);
        float distance = direction.magnitude;

        return(ProjectileMath.CalculateLaunchSpeed(distance, yOffset, Physics.gravity.magnitude, angle * Mathf.Deg2Rad));
    }
        public void TestFindInitialVelocity3()
        {
            Vector2 initialVelocity = new Vector2(1, 10);
            float   gravity         = -5f;
            Vector2 point           = CalculateProjectilePosition(initialVelocity, gravity, 10f);

            Tuple <Vector2, Vector2> solutions;

            solutions = ProjectileMath.FindInitialVelocity(initialVelocity.magnitude, point, gravity);

            Assert.IsNotNull(solutions);
            Assert.IsTrue(TestUtils.Approx(solutions.Item1, initialVelocity) || TestUtils.Approx(solutions.Item2, initialVelocity));
        }
Ejemplo n.º 6
0
    public void SetTargetWithBothAngleAndSpeed(Vector3 point, float angle, float speed)
    {
        currentRadian = angle * Mathf.Deg2Rad;
        currentSpeed  = speed;

        Vector3 direction = point - firePoint.position;
        float   yOffset   = direction.y;

        direction = Math3d.ProjectVectorOnPlane(Vector3.up, direction);
        float distance = ProjectileMath.CalculateLandDistance(firePoint.position.y, Physics.gravity.magnitude, angle, speed);

        projectileArc.UpdateArc(speed, distance, Physics.gravity.magnitude, currentRadian, direction, true);
        SetThrowPoint(direction, currentRadian * Mathf.Rad2Deg);
    }
    public void SetTargetWithAngle(Vector3 point, float angle)
    {
        currentAngle = angle;

        Vector3 direction = point - firePoint.position;
        float   yOffset   = -direction.y;

        direction = Math3d.ProjectVectorOnPlane(Vector3.up, direction);
        float distance = direction.magnitude;

        currentSpeed = ProjectileMath.LaunchSpeed(distance, yOffset, Physics.gravity.magnitude, angle * Mathf.Deg2Rad);

        projectileArc.UpdateArc(currentSpeed, distance, Physics.gravity.magnitude, currentAngle * Mathf.Deg2Rad, direction, true);
        SetTurret(direction, currentAngle);
    }
Ejemplo n.º 8
0
    public void UpdateArc(float speed, float distance, float gravity, float angle, Vector3 direction, bool valid)
    {
        Vector2[] arcPoints = ProjectileMath.ProjectileArcPoints(iterations, speed, distance, gravity, angle);
        Vector3[] points3d  = new Vector3[arcPoints.Length];

        for (int i = 0; i < arcPoints.Length; i++)
        {
            points3d[i] = new Vector3(0, arcPoints[i].y, arcPoints[i].x);
        }

        lineRenderer.positionCount = arcPoints.Length;
        lineRenderer.SetPositions(points3d);
        transform.rotation          = Quaternion.LookRotation(direction);
        lineRenderer.material.color = valid ? initialColor : errorColor;
    }
    public void SetTargetWithSpeed(Vector3 point, float speed, bool useLowAngle)
    {
        currentSpeed = speed;

        Vector3 direction = point - firePoint.position;
        float   yOffset   = direction.y;

        direction = Math3d.ProjectVectorOnPlane(Vector3.up, direction);
        float distance = direction.magnitude;

        float angle0, angle1;
        bool  targetInRange = ProjectileMath.LaunchAngle(speed, distance, yOffset, Physics.gravity.magnitude, out angle0, out angle1);

        if (targetInRange)
        {
            currentAngle = useLowAngle ? angle1 : angle0;
        }

        projectileArc.UpdateArc(speed, distance, Physics.gravity.magnitude, currentAngle, direction, targetInRange);
        SetTurret(direction, currentAngle * Mathf.Rad2Deg);
    }
Ejemplo n.º 10
0
        private void Start()
        {
            ProjectileMath.SetTargetWithAngle(transform.position, target.position, angle, out var speed, out var t, arcRenderer);

            if (gravityType == GravityType.Simulate)
            {
                var dir = is2D ? transform.right : transform.forward;
                _horizontalSpeed = dir * speed;
            }
            else
            {
                if (is2D)
                {
                    GetComponent <Rigidbody2D>().velocity = transform.right * speed;
                }
                else
                {
                    GetComponent <Rigidbody>().velocity = transform.forward * speed;
                }
            }
        }