0
\$\begingroup\$

I using CharacterController and not using a Rigidbody so I writing a script for downward velocity which is velocity=-9.81*(time) and I declare my initial velocity as 12. Supposing from this calculation, the max height it can reach from the equation is roughly 6.7891 unit on y axis. By using the formulae distance travel=initial velocity*(time)-0.5g*(time)^2,differentiate this w.r.t time, getting time=u/g=12/9.81=1.223, then max distance travel=12*(1.223)-0.5*(-9.81)*(1.223)^2=7.339 but from the coordinate below, can clearly seen the max distance travel before falling down is 13.2803unit--0.5499unit=13.8302unit? This is far exceed calculated value, wonder what went wrong?

Initial coordinate of y on ground=-0.5499 enter image description here

approximate final coordinate of y=13.2803 enter image description here

using UnityEngine;

public class NewPlayerMovement : MonoBehaviour
{
    public CharacterController Controller;
    //speed of the character
    public float Speed = 12f;
    // earth gravitational value
    public float Gravity = -9.81f;
    Vector3 velocity;
    //hit point of character
    float hp = 90;

    bool Grounded = true;

    public void OnControllerColliderHit(ControllerColliderHit hit)
    {
        Grounded = true;
        hp -= velocity.magnitude;
        velocity.y = 0;
        Debug.Log(hp);
    }

    public void OnControllerColliderExit(ControllerColliderHit hit)
    {
        Grounded = false;
    }

    public void Checkstatus()
    {
        if(Grounded==false)
        {
            velocity.y += 0.5f * Gravity * Time.deltaTime;
        }
    }

    void Update()
    {
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        Vector3 move = transform.right * x + transform.forward * z;

        Controller.Move(move * Speed * Time.deltaTime);

        if (Input.GetKeyDown(KeyCode.Space) && velocity.y == 0)
        {
            velocity.y = 12;
            Grounded = false;
        }

       //check if it grounded
        Checkstatus();
        Controller.Move(velocity * Time.deltaTime);
    }
}
\$\endgroup\$
6
  • \$\begingroup\$ Wouldn't the velocity be v.y += g * dt? I.e., without the 0.5 * (assuming g is negative). \$\endgroup\$ – Olivier Jacot-Descombes Feb 8 at 18:09
  • \$\begingroup\$ I using value of g=-9.81,putting negative again isn't that become positive? \$\endgroup\$ – chuackt Feb 8 at 18:11
  • \$\begingroup\$ I saw too late that you have a negative g. The problem is the multiplication with 0.5 because delta v = acceleration * t. The other calculations seem right to me. \$\endgroup\$ – Olivier Jacot-Descombes Feb 8 at 18:12
  • \$\begingroup\$ yeah I agree v=(acceleration)*t but if you look at the operation above, v.y=v.y+0.5*gt then at the bottom, I have one more command line Controller.Move(velocity * Time.deltaTime); , isn't the value of (velocityt)=distance = (v.y-0.5*g*t)*t? which is same as the physics equation distance=(initial velocity)*t-(0.5g)*(t)*(t)? Indeed this part I'm bit of confuse. \$\endgroup\$ – chuackt Feb 8 at 18:26
  • \$\begingroup\$ The equation with the 0.5 g t^2 is for the travel. But with v.y += g * dt you calculate the velocity. Then velocity * Time.deltaTime is a travel again. Note that we are working with linear approximations and summing is not the same as integrating. The velocity is constant during a given time step in the game. So the velocity curve looks like stairs, not like a sequence of infinitesimal small triangles approximating a smooth curve where the 0.5 comes from (baseline * height / 2). \$\endgroup\$ – Olivier Jacot-Descombes Feb 8 at 18:47
1
\$\begingroup\$

Calculating the new velocity should be

velocity.y += Gravity * Time.deltaTime;

without the 0.5f *. This is because the velocity remains the same during the whole time step unlike in the continuous case where we have an inclined velocity curve approximated with infinitesimal small triangles calculated as baseline * height / 2. This is where the 0.5 comes from when we integrate.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.