I’m creating a basketball game. I have a problem where if the user presses r, it returns the ball back to the user. The problem is after multiple tries, the ball has a physic problem and I’ve noticed the thrust increases more and more after I press r. Can someone help me?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public GameObject basketball;
Rigidbody basketballRB;
Camera FPSCam;
private bool holdingBall;
public float thrust;
void Start()
{
basketballRB = basketball.GetComponent<Rigidbody>();
FPSCam = GetComponentInChildren<Camera>();
holdingBall = true;
}
void FixedUpdate()
{
if (holdingBall)
{
basketballRB.useGravity = false;
basketball.transform.position = FPSCam.transform.position;
}
if (Input.GetKeyDown("mouse 0") && holdingBall)
{
Shoot(thrust);
holdingBall = false;
basketballRB.useGravity = true;
}
if (Input.GetKey("r"))
{
holdingBall = true;
}
}
public void Shoot(float thrust)
{
basketballRB.AddForce(FPSCam.transform.forward * thrust);
}
}