UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Vector3.ProjectOnPlane
# [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html).ProjectOnPlane public static [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) **ProjectOnPlane** ([Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) **vector**, [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) **planeNormal**); ## 参数 | planeNormal | 从矢量到平面的方向。 | | ------------- | ------------------------ | | vector | 平面上方的矢量的位置。 | ## 返回 **Vector3** 平面中的矢量的位置。 ## 描述 将向量投影到由法线定义的平面上(法线与该平面正交)。 [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) 将给定 [vector](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3-vector.html) 的位置存储在 3D 空间中。另一个 [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) 由 [planeNormal](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3-planeNormal.html) 提供,用于定义通过原点从平面到 [vector](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3-vector.html) 的方向。[Vector3.ProjectOnPlane](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.ProjectOnPlane.html) 使用两个 [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) 值在 [planeNormal](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3-planeNormal.html) 方向生成 [vector](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3-vector.html) 的位置,并返回 [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) 在平面上的位置。  *矢量从红色圆圈处开始,在黑色圆圈(代表平面法线)处结束。* 以下脚本示例使 `Update` 生成一个 `vector` 位置和一个 `planeNormal` 法线。[Vector3.ProjectOnPlane](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.ProjectOnPlane.html) 静态方法可接收参数并返回 [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html) 位置。 ``` using System.Collections; using System.Collections.Generic; using UnityEngine; // Vector3.ProjectOnPlane - example // Generate a random plane in xy. Show the position of a random // vector and a connection to the plane. The example shows nothing // in the Game view but uses Update(). The script reference example // uses Gizmos to show the positions and axes in the Scene. public class Example : MonoBehaviour { private Vector3 vector, planeNormal; private Vector3 response; private float radians; private float degrees; private float timer = 12345.0f; // Generate the values for all the examples. // Change the example every two seconds. void Update() { if (timer > 2.0f) { // Generate a position inside xy space. vector = new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), 0.0f); // Compute a normal from the plane through the origin. degrees = Random.Range(-45.0f, 45.0f); radians = degrees * Mathf.Deg2Rad; planeNormal = new Vector3(Mathf.Cos(radians), Mathf.Sin(radians), 0.0f); // Obtain the ProjectOnPlane result. response = Vector3.ProjectOnPlane(vector, planeNormal); // Reset the timer. timer = 0.0f; } timer += Time.deltaTime; } // Show a Scene view example. void OnDrawGizmosSelected() { // Left/right and up/down axes. Gizmos.color = Color.white; Gizmos.DrawLine(transform.position - new Vector3(2.25f, 0, 0), transform.position + new Vector3(2.25f, 0, 0)); Gizmos.DrawLine(transform.position - new Vector3(0, 1.75f, 0), transform.position + new Vector3(0, 1.75f, 0)); // Display the plane. Gizmos.color = Color.green; Vector3 angle = new Vector3(-1.75f * Mathf.Sin(radians), 1.75f * Mathf.Cos(radians), 0.0f); Gizmos.DrawLine(transform.position - angle, transform.position + angle); // Show a connection between vector and response. Gizmos.color = Color.yellow; Gizmos.DrawLine(vector, response); // Now show the input position. Gizmos.color = Color.red; Gizmos.DrawSphere(vector, 0.05f); // And finally the resulting position. Gizmos.color = Color.black; Gizmos.DrawSphere(response, 0.05f); } } ```
da
2022年6月22日 10:57
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码