RaycastHit.normal
Declaration
public Vector3 normal;Описание
Норма поверхности, на которую ударил луч.
Нормальная поверхности, на которую попадает луч на коллайдере в точке пересечения луча, которая может совпадать или не совпадать с исходной поверхностью сетки в зависимости от типа коллайдера и его настроек. Для примитивных коллайдеров, таких как BoxCollider или SphereCollider, нормаль рассчитывается на основе их простой геометрической формы. Для MeshCollider, если конвекс установлен на false (не выпуклый), Unity может возвратить фактическую интерполированную норму от поверхности сетки в точке попадания. Если конвекс установлен на true, нормаль вместо этого аппроксимируется от выпуклого корпуса сетки.
using UnityEngine; using UnityEngine.InputSystem;
public class Example : MonoBehaviour { // Calculate the reflection of a "laser beam" off a clicked object.
// The object from which the beam is fired. The incoming beam will // not be visible if the camera is used for this! Transform gunObj;
void Start() { gunObj = this.GetComponent<Transform>(); } void Update() { if (Mouse.current.leftButton.isPressed) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
if (Physics.Raycast(ray, out hit)) { // Find the line from the gun to the point that was clicked. Vector3 incomingVec = hit.point - gunObj.position;
// Use the point's normal to calculate the reflection vector. Vector3 reflectVec = Vector3.Reflect(incomingVec, hit.normal);
// Draw lines to show the incoming "beam" and the reflection. Debug.DrawLine(gunObj.position, hit.point, Color.red); Debug.DrawRay(hit.point, reflectVec, Color.green); } } } }
Дополнительные ресурсы: ,Physics.Raycast,, ,Physics.Linecast,, ,Physics.RaycastAll,.