gpt4 book ai didi

c# - 这个 unity3d c# 代码是什么意思?

转载 作者:太空狗 更新时间:2023-10-30 01:34:39 25 4
gpt4 key购买 nike

Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 newDotPosition =
mouseRay.origin - mouseRay.direction / mouseRay.direction.y * mouseRay.origin.y;

我看到 newDotPosition 是游戏对象在地形上的位置。但是下面的代码是做什么的呢?

mouseRay.direction / mouseRay.direction.y * mouseRay.origin.y

最佳答案

其实就是一些基本的几何学。

mouseRay.origin 是相机的世界位置。所以 mouseRay.origin.y 是相机的高度。

mouseRay.direction/mouseRay.direction.y * mouseRay.origin.y 的结果是下图中的红色矢量。它只是在地面上一直缩放 mouseRay.direction

然后它执行 mouseRay.origin - result 以在世界坐标中获取地面上的命中点。

enter image description here

如果有人感兴趣,这里是查看结果的代码。

public class Test : MonoBehaviour
{
public float originGizmoRadius = 1f;
public float newDotPositionGizmoSize = 0.5f;
public float directionLength = 2f;

Vector3 origin;
Vector3 direction;
Vector3 newDotPosition;

void Update()
{
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);

origin = mouseRay.origin;
direction = mouseRay.direction;

newDotPosition = origin - direction / direction.y * origin.y;
}

void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawSphere(origin, originGizmoRadius);
Gizmos.DrawLine(origin, origin + (direction.normalized * directionLength));

Gizmos.color = Color.green;
Gizmos.DrawCube(newDotPosition, newDotPositionGizmoSize * Vector3.one);
}
}

关于c# - 这个 unity3d c# 代码是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29875962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com