- Home /
Sometimes the ball passes through the object
ballForce =210
Gravity Y = -18
Kind people please help
public class GameManager : MonoBehaviour {
public Material[] balls;
public static GameManager instance;
public GameObject ball;
public Transform target;
public float ballForce;
Plane plane = new Plane (Vector3.forward,0);
public bool readyToshoot;
void Awake()
{
if(instance == null)
{
instance = this;
} else
{
Destroy(this.gameObject);
}
}
void Start()
{
ball.SetActive(true);
readyToshoot = true;
}
private bool IsPointerOverUIObject() {
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
return results.Count > 0;
}
void FixedUpdate()
{
if(PlayerPrefs.GetInt("index")==0)
{
ball.GetComponent<MeshRenderer>().material = balls[0];
}
if(PlayerPrefs.GetInt("index")==1)
{
ball.GetComponent<MeshRenderer>().material = balls[1];
}
if(PlayerPrefs.GetInt("index")==2)
{
ball.GetComponent<MeshRenderer>().material = balls[2];
}
if(PlayerPrefs.GetInt("index")==3)
{
ball.GetComponent<MeshRenderer>().material = balls[3];
}
if(PlayerPrefs.GetInt("index")==4)
{
ball.GetComponent<MeshRenderer>().material = balls[4];
}
if(PlayerPrefs.GetInt("index")==5)
{
ball.GetComponent<MeshRenderer>().material = balls[5];
}
Vector3 dir = target.position - ball.transform.position;
if (!IsPointerOverUIObject() && Input.GetMouseButtonUp(0) && readyToshoot)
{
ball.GetComponent<Animator>().enabled = false;
ball.GetComponent<Rigidbody>().AddForce(dir * ballForce, ForceMode.Impulse);
readyToshoot = false;
}
float dist;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast (ray, out dist))
{
Vector3 point = ray.GetPoint(dist);
target.position = new Vector3(point.x, point.y, 0);
}
}
}
Comment
Answer by Olakehs · Jan 06, 2020 at 11:08 AM
What's worked for me is setting the rigidbody in the inspector's Collision Detection as 'Continuous Dynamic'
Find the Rigidbody component in the 'ball' object in the inspector, and find the drop down menu for 'Collision Detection', and select 'Continuous Dynamic'. See if that helps
Your answer
Follow this Question
Related Questions
How do I add diagonal force? 0 Answers
Problem reading/getting from rb.velocity value? 1 Answer
Face forward direction of movement 3 Answers
Rigidbody AddForce Problem ? 1 Answer
Make object tip over 1 Answer