版本:Unity 6 (6000.0)
语言English
  • C#

RaycastHit.normal

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们会阅读用户提出的每个建议更改,并在适用时进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

public Vector3 normal;

描述

射线击中的表面的法线。

using UnityEngine;

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 Update() { if (Input.GetMouseButton(0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

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); } } } }