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);
}
}
}
}