Last active
January 23, 2023 08:48
-
-
Save klaszlo8207/06feee9e30c86e50efbc07b0ec173771 to your computer and use it in GitHub Desktop.
Unity raycast demo, egy ismerősnek példaprogi
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 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