unity raycast tutorial code example
Example 1: create ray unity
Ray ray = new Ray(transform.position, transform.forward);
Example 2: how to create a ray unity
using UnityEngine;public class ExampleClass : MonoBehaviour
{
void Start()
{
Ray ray = new Ray(transform.position, transform.forward);
}
}
Example 3: nity raycast
using UnityEngine;
{
void FixedUpdate()
{
int layerMask = 1 << 8;
layerMask = ~layerMask; RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
Debug.Log("Did Hit");
}
else
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
Debug.Log("Did not Hit");
}
}
}
Example 4: unity3d raycast
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
void Update()
{
int layerMask = 1 << 8;
layerMask = ~layerMask;
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
Debug.Log("Did Hit");
}
else
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
Debug.Log("Did not Hit");
}
}
}