- Home /
Question by
cystemic · Feb 11, 2015 at 01:17 AM ·
raycastmouseraycastingmouseposition
Raycast2d not working C# :(
Hi guys, I'm trying to raycast my mouseclicks to see if I'm clicking something or not but it's returning Null everytime I do. Does anyone have any ideas? I've been trying to get this to work for too long and I feel like I might be overlooking something.
void FixedUpdate () {
if (Input.GetMouseButton (0)) {
RaycastHit2D hitInfo = Physics2D.Raycast(new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,Camera.main.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 50f);
if (hitInfo != null) {
print (hitInfo.transform);
}
}
}
Thanks in advance. :)
Comment
Answer by cystemic · Feb 11, 2015 at 01:49 AM
Nevermind, I got it to work. Here is a working copy if anybody wants it.
void Update () {
if (Input.GetMouseButton (0)) {
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), transform.forward, Mathf.Infinity);
if (hit) {
Debug.Log (hit.collider.collider2D.gameObject.name);
}
}
}
Your answer
Follow this Question
Related Questions
How to Limit Input.mousePosition or Raycast 1 Answer
Mouse Over Questions 1 Answer
Checking if object stopped being hit by Raycast while overlapping another object of the same type. 0 Answers
How to achieve a more accurate Mouse to WorldCoordinates & faster updating of object follow? 2 Answers
Mouse plane does not detect height 1 Answer