Skip to content

Instantly share code, notes, and snippets.

@klaszlo8207
Last active January 23, 2023 08:48
Show Gist options
  • Save klaszlo8207/06feee9e30c86e50efbc07b0ec173771 to your computer and use it in GitHub Desktop.
Save klaszlo8207/06feee9e30c86e50efbc07b0ec173771 to your computer and use it in GitHub Desktop.
Unity raycast demo, egy ismerősnek példaprogi
using UnityEngine;
namespace _MyScripts.Demos
{
public class Unity_RaycastDemo : MonoBehaviour
{
[SerializeField] private Vector3 from = Vector3.zero; //honnan, meliyk pontbol
[SerializeField] private Vector3 direction = Vector3.right; //mi az irany vektor?
[SerializeField, Range(1, 20)] private float distance = 10; //mi a szakasz távolsága
[SerializeField] private LayerMask layerMask = 0; //mi a layerMask, amin van, alapbol Default
private RaycastHit _hit;
private bool _raycast;
private void OnDrawGizmos()
{
var fr = transform.position + from; //kezdopont
var to = fr + direction * distance; //vegpont
Gizmos.color = Color.red;
//ez egy szakasz ugye, piros szinnel rajzolva
Gizmos.DrawLine(fr, to);
Gizmos.color = Color.green;
//ha van metszes, akkor az a resz zold szinu lesz
if (_raycast)
Gizmos.DrawLine(fr, _hit.point);
}
private void FixedUpdate()
{
var fr = transform.position + from; //kezdopont
_raycast = Physics.Raycast(fr, direction, out _hit, distance, layerMask);
//ha metszett egy objektumot, ami a layerMaskon van
if (_raycast)
{
var go = _hit.collider.gameObject;
Debug.Log("Metszette : " + go.name + " -et, " + go.tag + " tag-et " + _hit.distance + " tavolságra " + _hit.point + " pontban.");
}
else
{
Debug.Log("Nincs metszés.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment