预备知识:
Raycast:在场景中投下可与所有碰撞器碰撞的一条光线
注意:如果射线从碰撞体的内部或者背面打,Raycast不检测碰撞
激光制作:创建新材质,其shader改为Particles/Addtive并添加激光图片,再拖到LineRenderer组件的材质
LayerMask 只投射指定层的物体
Raycast在场景中投下可与所有碰撞器碰撞的一条光线。
RaycastAll在场景投射一条射线并返回所有碰撞。注意不保证顺序。
主要是两种语法:
1.Raycast(Ray ray)
2.Raycast(Vector3 origin,Vector3 direction)
点哪移动到哪
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using UnityEngine; using System.Collections; /// <summary> 射线从摄像机发出,另一点为鼠标在世界坐标系的位置/// </summary> public class RayTest : MonoBehaviour { private Ray ray ; private RaycastHit hit;//射线碰到的碰撞信息 private NavMeshAgent agent; public Camera mapCamera;//小地图摄像机 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.collider.tag == "Plane") { agent.SetDestination(hit.point); Debug.DrawLine(ray.origin, hit.point, Color.red); } } } }
射线拾取物体
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 (Input.GetMouseButtonDown(0)) { flag = !flag; } if (Physics.Raycast(ray, out hit, 30, layer.value) && flag == true) { carryObj = hit.transform;//记录拾取到的物体 carryObj.parent = carryParent;//认父物体 carryObj.localPosition = Vector3.zero; carryObj.localEulerAngles = Vector3.zero; } if (flag == false) { carryObj.parent = null;//取消父子关系 //锁定x z 轴角度 carryObj.eulerAngles = new Vector3(0, carryObj.eulerAngles.y, 0); } } }
从枪口位置形成射线,准心效果
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using UnityEngine; using System.Collections; /// <summary> /// 从枪口位置形成射线,准心效果 /// </summary> public class TransRay : MonoBehaviour { private RaycastHit hit;//射线碰到的碰撞信息 public LayerMask layer; public Transform gunPos;//枪口位置 public RectTransform UIPos;//准心 private void Update () { if (Physics.Raycast(gunPos.position, gunPos.forward, out hit, 10, layer.value)) { Vector3 UIPoint = Camera.main.WorldToScreenPoint(hit.point); UIPos.gameObject.SetActive(true); UIPos.position = UIPoint; Debug.DrawLine(gunPos.position, hit.point, Color.red); } else UIPos.gameObject.SetActive(false); } }
从枪口位置形成射线,准心,激光效果
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using UnityEngine; using System.Collections; /// <summary> /// 从枪口位置形成射线,准心,激光效果 /// </summary> public class TransRay : MonoBehaviour { private RaycastHit hit;//射线碰到的碰撞信息 public LayerMask layer; public Transform gunPos;//枪口位置 public RectTransform UIPos;//准心 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)) { Vector3 UIPoint = Camera.main.WorldToScreenPoint(hit.point); UIPos.gameObject.SetActive(true); UIPos.position = UIPoint; //线渲染控制 line.enabled = true;//单独组件勾选 //设置位置 line.SetPosition(0, gunPos.position); line.SetPosition(1, hit.point); Debug.DrawLine(gunPos.position, hit.point, Color.red); } else { UIPos.gameObject.SetActive(false); line.enabled = false; } } }
球形射线
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using UnityEngine; using System.Collections; public class RayOther : MonoBehaviour { private RaycastHit[] hits; public LayerMask layer; private Collider[] colliders; private void Update () { //球形射线 colliders = Physics.OverlapSphere(transform.position, 5, layer.value); if(Input.GetMouseButtonDown(0)) { foreach (var collider in colliders) { Debug.DrawLine(transform.position, collider.transform.position, Color.red); collider.GetComponent<Renderer>().material.color = Color.green; collider.GetComponent<Score>().blood -= 50; } } } }