Last active
August 29, 2015 14:18
-
-
Save unitycoder/76645525b612c9d03456 to your computer and use it in GitHub Desktop.
Physics2D.Raycast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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