C# 在透视模式下绘制调试光线
我已经成功地使用正交摄影机投射和绘制调试光线,但我将其更改为“透视”,并且我的光线似乎不再有效(在场景视图中,调试光线不会随鼠标移动) 这是我的正字法代码,我需要做什么不同的透视C# 在透视模式下绘制调试光线,c#,unity3d,raytracing,C#,Unity3d,Raytracing,我已经成功地使用正交摄影机投射和绘制调试光线,但我将其更改为“透视”,并且我的光线似乎不再有效(在场景视图中,调试光线不会随鼠标移动) 这是我的正字法代码,我需要做什么不同的透视 public class Cursor : MonoBehaviour { // 45 degree angle down, same as camera angle private Vector3 gameAngle = new Vector3(0, -45, -45); public RaycastHit hit
public class Cursor : MonoBehaviour {
// 45 degree angle down, same as camera angle
private Vector3 gameAngle = new Vector3(0, -45, -45);
public RaycastHit hit;
public Ray ray;
void Update() {
// Rays
ray = new Ray(Camera.main.ScreenToWorldPoint(Input.mousePosition), gameAngle);
if (Debug.isDebugBuild)
Debug.DrawRay(Camera.main.ScreenToWorldPoint(Input.mousePosition), gameAngle * 20, Color.green);
}
}
你试过改变观点吗?我假设您打开了2d模式。我想直接从文档中找到答案
首先,我们需要知道DrawRay函数期望从参数中得到什么 它需要光线的原点、方向和距离(也可以为其指定颜色和其他参数) 公共静态void DrawRay(Vector3 start,Vector3 dir,Color Color=Color.white,float duration=0.0f,bool depthTest=true) 所以现在我们需要知道射线原点和射线点位置之间的方向,我们可以使用减法 如果将空间中的一个点与另一个点相减,则结果为a 从一个对象“指向”另一个对象的向量: 现在有了这些信息,我们可以得到光线的方向和距离
var distance = heading.magnitude;
var direction = heading / distance; // This is now the normalized direction.
这将是生成的代码
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
// Gets a vector that points from the player's position to the target's.
var heading = hit.point - ray.origin;
var distance = heading.magnitude;
var direction = heading / distance; // This is now the normalized direction.
Debug.DrawRay(ray.origin, direction * distance, Color.red);
}
var distance = heading.magnitude;
var direction = heading / distance; // This is now the normalized direction.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
// Gets a vector that points from the player's position to the target's.
var heading = hit.point - ray.origin;
var distance = heading.magnitude;
var direction = heading / distance; // This is now the normalized direction.
Debug.DrawRay(ray.origin, direction * distance, Color.red);
}