Code Doesnt work

my soccer/football when i hold LMB it doesnt shoot the ball maximum power is 30 and script placed in the player which is only a capsule with a movement script

script here

using UnityEngine;

public class Shot : MonoBehaviour
{
    public GameObject soccerBall; // Reference to your soccer ball
    public float maxPower = 100f; // Maximum power for the shot
    private float power = 0f;
    private bool charging = false;

    void Update()
    {
        if (Input.GetMouseButtonDown(0)) // Left mouse button down
        {
            charging = true;
            power = 0f;
        }

        if (charging)
        {
            power += Time.deltaTime * 100; // Increase power over time
            if (power > maxPower)
            {
                power = maxPower;
            }

            // Optional: Add visual feedback for power here
        }

        if (Input.GetMouseButtonUp(0)) // Left mouse button up
        {

            charging = false;
            ShootBall();
            power = 0f;
        }
    }

    void ShootBall()
    {
        if (soccerBall != null)
        {
            Rigidbody rb = soccerBall.GetComponent<Rigidbody>();
            if (rb != null)
            {
                Vector3 direction = (Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z)) - soccerBall.transform.position).normalized;
                rb.AddForce(direction * power, ForceMode.Impulse);
            }
        }
    }
}
1 Like
  1. Try to delete the 2nd parameter in AddForce(force), so such way you set the mode as ForceMode.Force by default. This method works in my Football project.
  2. What are your Rigidbody Settings?
1 Like

oh the error is in line 44 but i fixed it but now it works trashly

1 Like

Paste the code in the original post correctly, there is no way to define the line 44 for now.

1 Like

typed correctly, don’t bother urself fixing line 44 cus i fixed it but the ball is getting shot even tho i aint looking at it or being close to it