Drawing projectile arc: don't know how it work.

Hello every one.

I have these code to draw a projectile arc.

Note: in my code:

  • weaponReady: my bullet, , on the turret, world coordinate.
  • drawDots: is a list of dots to render in the screen.
  • There are 3 ways #1 #2 #3 to achieved that, the results is exactly the same.

Question: why it work correctly when I product the gravity to 10?

Thank you all.

private void UpdateDrawer()
    {
        this.panelDrawer.gameObject.SetActive(true);

        Vector3 startPos = this.weaponSpawnPoint.transform.position;

        Vector3 startVelocity = this.weaponReady.transform.right * this.dragRatio * this.weaponReady.Phys.MaxVelocity;
        float gravity = Physics.gravity.y * this.weaponReady.Phys.Rigid.gravityScale * 10f;// why *10 work correctly???


        // // #3 fake an object // lag as hell
        //GameObject go = new GameObject();
        //Transform goTrans = go.transform;
        //goTrans.SetParent(this.panelDrawer);
        //Rigidbody2D goRigid = go.AddComponent<Rigidbody2D>();
        //goRigid.isKinematic = false;
        //goRigid.velocity = startVelocity;
        //goTrans.position = startPos;



        Vector2 pos = startPos;
        Vector2 vel = startVelocity;

        float period = Time.fixedDeltaTime;

        //float time = 0f;

        // // #3
        //Physics2D.autoSimulation = false;

        for (var i = 0; i < this.numDrawDot; ++i)
        {
            // // #1
            vel.y = vel.y + gravity * period;
            pos = pos + vel * period;
            this.drawDots[i].position = new Vector3(pos.x, pos.y, 0f);

            // // #2
            //time += period;
            //this.drawDots[i].position = startPos + startVelocity * time + Physics.gravity * time * time * 0.5f;

            // // #3
            //Physics2D.Simulate(period);
            //this.drawDots[i].position = goTrans.position;
        }

        // // #3
        //Physics2D.autoSimulation = true;

    }

How stupid am I. I set the gravity scale to 10f in somewhere in the project, but the code above was run before the gravity scale be changed.

1 Like