Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

Sometimes the ball passes through the object

$
0
0
- 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 results = new List(); EventSystem.current.RaycastAll(eventDataCurrentPosition, results); return results.Count > 0; } void FixedUpdate() { if(PlayerPrefs.GetInt("index")==0) { ball.GetComponent().material = balls[0]; } if(PlayerPrefs.GetInt("index")==1) { ball.GetComponent().material = balls[1]; } if(PlayerPrefs.GetInt("index")==2) { ball.GetComponent().material = balls[2]; } if(PlayerPrefs.GetInt("index")==3) { ball.GetComponent().material = balls[3]; } if(PlayerPrefs.GetInt("index")==4) { ball.GetComponent().material = balls[4]; } if(PlayerPrefs.GetInt("index")==5) { ball.GetComponent().material = balls[5]; } Vector3 dir = target.position - ball.transform.position; if (!IsPointerOverUIObject() && Input.GetMouseButtonUp(0) && readyToshoot) { ball.GetComponent().enabled = false; ball.GetComponent().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); } } }

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>