0

In the function below, The distance between ball and target is known(R). Also, the angle between the resultant vector and the x-axis is known(LaunchAngle). Thanks to these parameters(R, LaunchAngle), I calculate the initial velocity of a ball. I checked all the values. According to physics, they are all correct. Although all calculations are correct, the ball does not hit the target.

void LaunchFromTargetPositionWithoutFrictionForce()
        {
            Vector3 projectileXZPos = new Vector3(transform.position.x, 0.0f, transform.position.z);
            Vector3 targetXZPos = new Vector3(TargetObjectTF.position.x, 0.0f, TargetObjectTF.position.z);
            transform.LookAt(targetXZPos);

            float R = Vector3.Distance(projectileXZPos, targetXZPos);
            float G = -Physics.gravity.y;

            float Vz = Mathf.Sqrt(G * R / Mathf.Sin((2.0f * LaunchAngle) * Mathf.Deg2Rad));
            float Vy = Vz * Mathf.Sin(LaunchAngle * Mathf.Deg2Rad);
            float Vx = Vz * Mathf.Cos(LaunchAngle * Mathf.Deg2Rad);
            text2.text = "vz: " + Vz.ToString() + " vy: " + Vy.ToString() + " vx: " + Vx.ToString();

            Vector3 localVelocity = new Vector3(0f, Vy, Vx);
            Vector3 globalVelocity = transform.TransformDirection(localVelocity);


            rigid.velocity = globalVelocity;
            bTargetReady = true;

            if (isSlowMotion) timeManager.slowMotion();
        }

First location of the ball first location of ball

And after 2-dimensional motion it is hit before target 2d motion befor target hit

nkazz
  • 488
  • 1
  • 8
  • 13
  • maybe i understand it wrong, but it seems like the target is on course? does it stop befor the target? is there a collider that is to big? – nkazz Feb 14 '20 at 07:22
  • according to the angle, sometimes it stops before the target, sometimes after the target, and sometimes stops on exact target. But it should stop on target every time. There is a collider. – Ayşe Betül Şimşek Feb 14 '20 at 08:03
  • does rigidbody using diffrent physics? According to the physic calculations it should hit target every time. But it hits somewhere else what i calculated?? – Ayşe Betül Şimşek Feb 14 '20 at 08:11
  • What is the difference between stop points on axis ? Like sometimes it stops at 10.000134f and sometimes it stops at 10.000256f ? Numbers are just example but what I've experienced is dealing with precision is hard. Maybe try to fix the positions and numbers to .00f that might help – Thalthanas Feb 14 '20 at 08:19
  • the difference is not small. It is like 10f and - 13f. – Ayşe Betül Şimşek Feb 14 '20 at 08:26

1 Answers1

0

I changed first 3 line with below codes. And problem solved.

    Vector3 projectileXZPos = transform.position;
    Vector3 targetXZPos = TargetObjectTF.position;
    float dist = Vector3.Distance(projectileXZPos, targetXZPos);
    transform.LookAt(targetXZPos);