#bullet problem?

1 messages · Page 1 of 1 (latest)

cloud phoenix
#
public Transform target;
    public float shootForce = 200f;

    private void FixedUpdate()
    {
        Vector3 initialDirection = target.position - transform.position;

        float initialHorizontalDistance = new Vector3(initialDirection.x, 0f, initialDirection.z).magnitude;

        float timeOfFlight = initialHorizontalDistance / shootForce;

        float verticalVelocity = initialDirection.y / timeOfFlight + 0.5f * Mathf.Abs(Physics.gravity.y) * timeOfFlight;

        Vector3 initialVelocity = new Vector3(initialDirection.x, verticalVelocity, initialDirection.z).normalized * shootForce;

        Vector3 targetFuturePosition = target.position + target.GetComponent<Rigidbody>().velocity * timeOfFlight;

        Vector3 adjustedDirection = targetFuturePosition - transform.position;

        float adjustedHorizontalDistance = new Vector3(adjustedDirection.x, 0f, adjustedDirection.z).magnitude;

        float adjustedTimeOfFlight = adjustedHorizontalDistance / shootForce;

        float adjustedVerticalVelocity = adjustedDirection.y / adjustedTimeOfFlight + 0.5f * Mathf.Abs(Physics.gravity.y) * adjustedTimeOfFlight;

        Vector3 adjustedInitialVelocity = new Vector3(adjustedDirection.x, adjustedVerticalVelocity, adjustedDirection.z).normalized * shootForce;

        float horizontalAngle = Mathf.Atan2(adjustedDirection.x, adjustedDirection.z) * Mathf.Rad2Deg;

        float verticalAngle = Mathf.Asin(adjustedVerticalVelocity / shootForce) * Mathf.Rad2Deg;

        transform.rotation = Quaternion.Euler(-verticalAngle, horizontalAngle, 0f);
    }

here is my code, i am kinda having am problem aiming up and down

#

and im getting this error when the target is too high/low

tall gorge
#

verticalAngle or horizontalAngle must be near infinity

#

Also, I was expecting to see a sin and cos at some place. Not exactly sure what you did to find your formula as you did not leave any trace.

What are the original formula you are using ? For reference, here is script of doing Angle/MaxDistance

#archived-code-advanced message

#

In your situation, you could take the same formula and isolate the angle instead of the velocity.

cloud phoenix