Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active August 29, 2015 14:18
Show Gist options
  • Save unitycoder/76645525b612c9d03456 to your computer and use it in GitHub Desktop.
Save unitycoder/76645525b612c9d03456 to your computer and use it in GitHub Desktop.
Physics2D.Raycast
using UnityEngine;
using System.Collections;
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment