简单总结Unity学习笔记——射线Ray

射线的原理是从一个起始点,向一个方向(矢量)发射一条(无限长/规定长度)射线。

unity里面射线分为两种,一种为穿透性的射线RaycastAll,一种为不穿透的射线Raycast。

个人对Raycast分别做了在Start()和Update()里发射射线的测试

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

public class Ray : MonoBehaviour {
    Ray ray;
    RaycastHit hit;

	// Use this for initialization
	void Start () {
        Physics.Raycast(new Vector3(0, 0, 0), new Vector3(0, -1, 0), out hit);
        Debug.Log(hit);
        //Debug.DrawLine(new Vector3(0, 0, 0), hit.point, Color.red);
    }
	
	// Update is called once per frame
	void Update () {
        //Physics.Raycast(new Vector3(0, 0, 0), new Vector3(0, -1, 0), out hit);
        //Debug.Log(hit.collider.name);
        Debug.DrawLine(new Vector3(0, 0, 0), hit.point, Color.red);
    }
}

在上面的代码里,start()里的射线发射了一次,碰撞到第一个物体后,hit接收的物体就不会再改变,如下图红色射线在panel1移开后也不会继续伸长

而在Update()里发射射线,当panel1移开后,射线会继续伸长到直到与panel2发生碰撞

总结:Raycast发射的射线是不具有穿透性的,并且在与一个物体发生碰撞后就会停止。

Raycast能应用于物品拾取,子弹、激光检测

以上观点纯属个人理解总结,有什么问题欢迎讨论。

猜你喜欢

转载自blog.csdn.net/qq_37111794/article/details/81084483