Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save unitycoder/76645525b612c9d03456 to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/76645525b612c9d03456 to your computer and use it in GitHub Desktop.

Revisions

  1. unitycoder revised this gist May 14, 2015. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions Physics2DRaycast.cs
    Original file line number Diff line number Diff line change
    @@ -4,17 +4,29 @@
    public class Test : MonoBehaviour {

    public LayerMask targetLayer;

    Vector3 prevPos;

    void Start () {
    }

    void Update ()
    {
    // to mouse point
    Ray mousePos = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit2D hit = Physics2D.Raycast(mousePos.origin, mousePos.direction,Mathf.Infinity, targetLayer);
    if (hit.collider != null)
    {
    Destroy(hit.transform.gameObject);
    }

    // from point to point
    Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,10));
    hit = Physics2D.Linecast(prevPos, mousePos);
    if (hit.collider != null)
    {
    Debug.Log("collided at:"+hit.point);
    }
    prevPos = mousePos;
    }
    }
  2. unitycoder renamed this gist May 11, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. unitycoder created this gist Apr 7, 2015.
    20 changes: 20 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    using UnityEngine;
    using System.Collections;

    public class Test : MonoBehaviour {

    public LayerMask targetLayer;

    void Start () {
    }

    void Update ()
    {
    Ray mousePos = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit2D hit = Physics2D.Raycast(mousePos.origin, mousePos.direction,Mathf.Infinity, targetLayer);
    if (hit.collider != null)
    {
    Destroy(hit.transform.gameObject);
    }
    }
    }