#ballcontroller help

1 messages · Page 1 of 1 (latest)

hollow flower
#

Can anyone help me with making the ball fly higher the further I pull back? It's not working as expected in my project. this is my code rn:
using UnityEngine;

public class BallController : MonoBehaviour
{
private Vector3 touchStartPosition;
private bool isCharging = false;
private float chargeStrength = 0f;
private Rigidbody ballRigidbody;

public float maxChargeStrength = 10f;
public float throwForceMultiplier = 10f;
public float zMultiplier = 1.5f;

void Start()
{
    ballRigidbody = GetComponent<Rigidbody>();
}

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        isCharging = true;
        touchStartPosition = Input.mousePosition;
        chargeStrength = 0f;
    }
    else if (Input.GetMouseButton(0))
    {
        if (isCharging)
        {
            chargeStrength = Mathf.Clamp(Vector3.Distance(Input.mousePosition, touchStartPosition), 0f, maxChargeStrength);
        }
    }
    else if (Input.GetMouseButtonUp(0))
    {
        if (isCharging)
        {
            isCharging = false;

            Vector3 throwDirection = (touchStartPosition - Input.mousePosition).normalized;

            ThrowBall(chargeStrength, throwDirection);
        }
    }
}

void ThrowBall(float chargeStrength, Vector3 throwDirection)
{
    float throwForce = chargeStrength * throwForceMultiplier;

    float throwForceY = throwForce;
    float throwForceZ = throwForce * zMultiplier;

    Vector3 throwForceVector = new Vector3(throwDirection.x, throwForceY, throwDirection.y * throwForceZ);

    ballRigidbody.AddForce(throwForceVector, ForceMode.Impulse);
}

}