Answers for "unity raycast script"

C#
21

raycast unity

RaycastHit hit;
        // Does the ray intersect any objects excluding the player layer
        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("Hit");
        }
        else
        {
            Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
            Debug.Log("No Hit");
        }
Posted by: Guest on December-24-2019
0

Unity raycast script

using UnityEngine;
public class ForRaycasting : MonoBehaviour
{
    
    void FixedUpdate()
    {
        int layerMask = 1 << 6;//Since Friend is layer number 6
        layerMask = ~layerMask; //Inverted to ignore layer
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 10,layerMask))
        {            
            Destroy(hit.collider.gameObject);
        }
        else
        {            
            Debug.Log("Did not Hit");
        }
    }
}
Posted by: GonaRip on January-08-2023

C# Answers by Framework

Browse Popular Code Answers by Language