unity3D Raycast的用法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DisplayInfo : MonoBehaviour
{
    public GameObject displayImage;
    public Text objectNameText;
    public LayerMask mask;

    private void FixedUpdate()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;

        if(Physics.Raycast(ray, out hitInfo, 200, mask))
        {
            if(hitInfo.collider != null)
            {
                Debug.DrawLine(transform.position, hitInfo.point, Color.red);
                displayImage.SetActive(true);
                objectNameText.text = hitInfo.collider.gameObject.name;
            }
        }
        else
        {
            objectNameText.text = "";
            displayImage.SetActive(false);
        }
    }
}

using UnityEngine;

public class Example01 : MonoBehaviour
{
    Ray ray;
    [SerializeField] private float maxDistance;//Weapon Attack Range
    public LayerMask mask;
    public Material highlightMat;

    private void Update()
    {
        ray = new Ray(transform.position, transform.forward);//Create a new RAY

        //RaycastHit hitInfo;//CORE STRUCT of ray original & ray direction (Store the information about eray hit the gameObject)

        //MARKER LayerMaks means we can only SHOT enemy in this layer mask
        //STEP 01 Single Attack
        //if(Physics.Raycast(ray, out hitInfo, maxDistance, mask, QueryTriggerInteraction.Ignore)) {
        //    Debug.Log(hitInfo.collider.gameObject.name);
        //    Debug.DrawLine(transform.position, hitInfo.point, Color.red);
        //}
        //else
        //{
        //    //Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100, Color.green);
        //    Debug.DrawRay(transform.position, transform.position + transform.forward * 100, Color.yellow);//ONLY view
        //}

        //STEP 02
        RaycastHit[] hits;
        hits = Physics.RaycastAll(ray, maxDistance);
        Debug.DrawLine(transform.position, transform.position + transform.forward * 20, Color.red);

        foreach(RaycastHit hit in hits)
        {
            hit.collider.gameObject.GetComponent<Renderer>().material = highlightMat;
        }

    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fire : MonoBehaviour
{
    public Material highlightMat;

    private void FixedUpdate()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if(Physics.Raycast(ray, out hit, 200))
        {
            if(hit.collider != null)
            {
                hit.collider.transform.GetComponent<Renderer>().material = highlightMat;
            }
        }
    }
}

using UnityEngine;

public class Placement : MonoBehaviour
{
    //public LayerMask planeMask;//OPTIONAL
    //public GameObject tree;

    //public GameObject selectedPrefab;

    //private void FixedUpdate()
    //{
    //    //MARKER Screen Space -> Ray 
    //    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    //    //RaycastHit hitInfo;

    //    if(Physics.Raycast(ray, out RaycastHit hitInfo, Mathf.Infinity, planeMask))
    //    {
    //        tree.transform.position = hitInfo.point;//The Cube will follow our Mouse
    //        tree.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);

    //        if (Input.GetMouseButtonDown(0))
    //        {
    //            Instantiate(tree, hitInfo.point, Quaternion.FromToRotation(Vector3.up, hitInfo.normal));
    //        }
    //    }
    //}

    public LayerMask planeMask;//OPTIONAL

    public GameObject selectedPrefab;

    private void FixedUpdate()
    {
        //MARKER Screen Space -> Ray 
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        //RaycastHit hitInfo;

        if (Physics.Raycast(ray, out RaycastHit hitInfo, Mathf.Infinity, planeMask))
        {
            selectedPrefab.transform.position = hitInfo.point;//The Cube will follow our Mouse
            selectedPrefab.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);

            if (Input.GetMouseButtonDown(0))
            {
                Instantiate(selectedPrefab, hitInfo.point, Quaternion.FromToRotation(Vector3.up, hitInfo.normal));
            }
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TreeButton : MonoBehaviour
{
    public GameObject treePrefab;

    public void PressButton()
    {
        FindObjectOfType<Placement>().selectedPrefab = treePrefab;
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class ClickMove : MonoBehaviour
{
    private NavMeshAgent navMeshAgent;
    private Ray ray;

    private void Start()
    {
        navMeshAgent = GetComponent<NavMeshAgent>();
    }

    private void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;

            if(Physics.Raycast(ray, out hitInfo, Mathf.Infinity))
            {
                navMeshAgent.SetDestination(hitInfo.point);
            }
        }

        Debug.DrawRay(Camera.main.transform.position, ray.direction * 100, Color.yellow);
    }
}

上一篇:【Unity3D】个人开发台球小游戏


下一篇:史上最全Unity3D游戏开发教程,从入门到精通(含学习路线图)