Created
October 18, 2019 13:19
-
-
Save takashi1975/95cc719354439d5056fd59488d545e4a to your computer and use it in GitHub Desktop.
RayCast
This file contains 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class RayHit : MonoBehaviour | |
{ | |
[SerializeField] | |
private GameObject _hitObject = null; | |
public GameObject HitObject { | |
get { return this._hitObject; } | |
private set { this._hitObject = value; } | |
} | |
public GameObject Ray(string layerName, float distance) { | |
var layerMask = LayerMask.NameToLayer(layerName); | |
return this.Ray(layerMask, distance); | |
} | |
public GameObject Ray(string[] layerNames, float distance) { | |
int layerMask = 0; | |
foreach (var name in layerNames) { | |
int layer = LayerMask.NameToLayer(name); | |
layerMask += layer; | |
} | |
return this.Ray(layerMask, distance); | |
} | |
public GameObject Ray(int layerMask, float distance) { | |
return this.Ray(layerMask, Input.mousePosition, distance); | |
} | |
public GameObject Ray(int layerMask, Vector3 screenPos, float distance) { | |
var hit_info = new RaycastHit(); | |
var ray = Camera.main.ScreenPointToRay(screenPos); | |
var direction = Vector3.forward; | |
var is_hit = Physics.Raycast(ray, out hit_info, distance, layerMask); | |
if (is_hit == true) { | |
this.HitObject = hit_info.transform.gameObject; | |
} else { | |
this.HitObject = null; | |
} | |
return this.HitObject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment