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();
}
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;
///
/// 小地图射线使用
///
public class MapNav : MonoBehaviour
{
private RaycastHit hit;
private Ray ray;
public Camera mapCamera;//小地图摄像机
private NavMeshAgent agent;
private void Start()
{
agent = this.GetComponent();
}
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;
///
/// 摄像机位置投放射线 准心效果
///
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;
///
/// 枪口位置投放射线 准心效果
///
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;
///
/// 枪口位置投放射线 准心效果
///
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();
}
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;
///
/// 射线拾取物体
///
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;
///
/// 球形射线和射线列表
///
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().material.color = Color.red;
hits[i].GetComponent().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().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);
}
}
}