2 de jun. de 2018

Parabole movement

How to make a GameObject jump as a Parabole.


public float height;
    public float duration;

    void Update () {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartCoroutine(Jump(height, duration));
        }
    }

    IEnumerator Jump(float height, float duration)
    {
        float lastY = transform.position.y;
        Vector3 startPos = transform.position;
        Vector3 endPos = transform.position;
        float time = 0f;

        while(time <= 1)
        {
            time += Time.deltaTime / duration;
            float yOffset = height * (time - (time * time));
            transform.position = Vector3.Lerp(startPos, endPos, time) + yOffset * Vector3.up;

            yield return null;
        }

        transform.position = new Vector3(transform.position.x, lastY, transform.position.z);
    }

Nenhum comentário: