- Home /
Question by
StefanWullems · Jul 16, 2017 at 11:53 AM ·
raycast2d-physicsrayraycasthit2d
How can I raycast in 2d
Hey, I'm making a 2D sort of top down gravity game. In which I have to cast a ray from the camera toward my playing field I had it running perfectly a while ago but converting the code to be a fit for touchscreen input makes things a bit complicated for me.
This is my code, it doesn't seem to progress towards my debug.log that's logging "3" no matter what I try
if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
bool isHit = Physics.Raycast(ray, Mathf.Infinity);
Debug.Log(ray.direction);
if (isHit) {
GameObject GOHit = hit.transform.gameObject;
touchList.Add(GOHit);
Debug.Log("3");
Comment
Your answer
Follow this Question
Related Questions
RayCast2d Not colliding with objects in other layers 1 Answer
I don't get Raycasts 0 Answers
What's wrong with my RaycastHit2D? 1 Answer
How to send a Ray through a vertex? 0 Answers
OnCollisionEnter2D is not working 0 Answers