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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值