unity射线

1.摄像机位置投放射线,鼠标点到哪移动到哪

using UnityEngine;
using System.Collections;

public class RayTest : MonoBehaviour
{
    private Ray ray;
    private RaycastHit hit;//射线碰到的碰撞信息
    private NavMeshAgent agent;
    private void Start() {
        agent = this.GetComponent<NavMeshAgent>();
    }
    void Update(){
        //起始位置(主摄像机)
        //方向(鼠标位置)
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, 100)&&
            Input.GetMouseButtonDown(0)){
            agent.SetDestination(hit.point);
            //ray.origin射线起始点
            // hit.point射线碰到碰撞器的接触点
            Debug.DrawLine(ray.origin, hit.point, Color.red);
        }
    }
    //鼠标点哪物体移动到哪,射线跟寻路配合
}

2.鼠标仅点击小地图才会移动到目标点

using UnityEngine;
using System.Collections;

/// <summary>
/// 小地图射线使用
/// </summary>
public class MapNav : MonoBehaviour
{
    private RaycastHit hit;
    private Ray ray;
    public Camera mapCamera;//小地图摄像机
    private NavMeshAgent agent;
    private void Start()
    {
        agent = this.GetComponent<NavMeshAgent>();
    }
    private void Update()
    {
        ray = mapCamera.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, 100) &&
            Input.GetMouseButtonDown(0))
        {
            if(hit.transform.tag == "House")
            agent.SetDestination(hit.transform.position);
        }
    }
 
}

3.摄像机投放射线实现准心效果

using UnityEngine;
using System.Collections;

/// <summary>
/// 摄像机位置投放射线 准心效果
/// </summary>
public class RayUI : MonoBehaviour
{
    public RectTransform UI;
    private RaycastHit hit;
    private Ray ray;
    public LayerMask layer;//遮罩层
    private void Update()
    {
       ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       //if (Physics.Raycast(ray, out hit, 50))
       if (Physics.Raycast(ray, out hit, 50, layer.value))
       {
          // if (hit.transform.tag == "target")
          // {
               UI.gameObject.SetActive(true);
               UI.position = Input.mousePosition;
           //}
           //else
            //   UI.gameObject.SetActive(false);
       }
        else
           UI.gameObject.SetActive(false);
    }
 
}

4.枪口位置投放射线实现准心效果

using UnityEngine;
using System.Collections;

/// <summary>
/// 枪口位置投放射线 准心效果
/// </summary>
public class GunUI : MonoBehaviour
{
    private RaycastHit hit;
    public LayerMask layer;
    public Transform gunPos;//枪口位置
    public RectTransform UI;//准心
    private void Update()
    {
        if (Physics.Raycast(gunPos.position,
            gunPos.forward, out hit, 10, layer.value))
        {
            //世界坐标转屏幕坐标
            Vector3 gunUI = Camera.main.WorldToScreenPoint(hit.point);
            UI.gameObject.SetActive(true);
            UI.position = gunUI;
            Debug.DrawLine(gunPos.position, hit.point, Color.red);
        }
        else
            UI.gameObject.SetActive(false);
    }
 
}

5.线渲染:添加Effect/linerender

using UnityEngine;
using System.Collections;

/// <summary>
/// 枪口位置投放射线 准心效果
/// </summary>
public class GunUI : MonoBehaviour
{
    private RaycastHit hit;
    public LayerMask layer;
    public Transform gunPos;//枪口位置
    public RectTransform UI;//准心
    private LineRenderer line;
    private void Start()
    {
        line = this.GetComponent<LineRenderer>();
    }
    private void Update()
    {
        if (Physics.Raycast(gunPos.position,
            gunPos.forward, out hit, 10, layer.value))   //起始位置,方向,距离,层   (不写距离为无限远,若不写距离但是写了layer.value则layer.value的值会被当成距离)
        {
            //世界坐标转屏幕坐标
            Vector3 gunUI = Camera.main.WorldToScreenPoint(hit.point);
            UI.gameObject.SetActive(true);
            UI.position = gunUI;
            //线渲染
            line.enabled = true;
            //设置线位置
            line.SetPosition(0, gunPos.position);
            line.SetPosition(1, hit.point);

            Debug.DrawLine(gunPos.position, hit.point, Color.red);
        }
        else
        {
            line.enabled = false;
            UI.gameObject.SetActive(false);
        }   
    }
}

6.射线拾取物体

using UnityEngine;
using System.Collections;

/// <summary>
/// 射线拾取物体
/// </summary>
public class RayCarry : MonoBehaviour
{
    private Ray ray;
    private RaycastHit hit;
    public LayerMask layer;
    public Transform carryParent;//拾取到物体的父物体
    private Transform carryObj;//拾取到的物体
    private bool flag = false;
    private void Update()
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (!flag)
        {
            if (Physics.Raycast(ray, out hit, 10, layer.value)
                && Input.GetMouseButtonDown(0))
            {
                flag = true;
                carryObj = hit.transform;
                carryObj.parent = carryParent;
                carryObj.localPosition = Vector3.zero;
                carryObj.localEulerAngles = Vector3.zero;
            }
        }
        else
        {
            if (Input.GetMouseButtonDown(0))
            {
                flag = false;
                //取消父子关系
                carryObj.parent = null;
                carryObj.eulerAngles = new Vector3(0, carryObj.eulerAngles.y, 0);
            }
        }

    }
 
}

7.其他种类射线

using UnityEngine;
using System.Collections;

/// <summary>
/// 球形射线和射线列表
/// </summary>
public class RayOther : MonoBehaviour
{
    //private RaycastHit[] hits;
    private Collider[] hits;
    public LayerMask layer;
    private void Update()
    {
    
        //球形射线
        if (Input.GetKeyDown(KeyCode.Space))
        {
            hits = Physics.OverlapSphere(transform.position, 3, layer.value);
            for (int i = 0; i < hits.Length; i++)
            {
                hits[i].transform.GetComponent<Renderer>().material.color = Color.red;
                hits[i].GetComponent<EnemyHp>().enemyHP -= 5;
                Debug.DrawLine(transform.position, hits[i].transform.position, Color.blue);
            }
        }

        //射线投射列表:返回所有碰撞不会被第一个碰撞层遮挡,返回顺序不固定

        hits = Physics.RaycastAll(transform.position, transform.right, 5);
        for (int i = 0; i < hits.Length; i++)
        {
            hits[i].transform.GetComponent<Renderer>().material.color = Color.red;
            Debug.DrawLine(transform.position, hits[i].point, Color.blue);
        }
      if (Physics.Raycast(transform.position,transform.right, out hit, 5))
      {
          Debug.DrawLine(transform.position, hit.point, Color.red);
      }
    } 
}
  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值