在透视模式下绘制调试射线
Draw debug ray in perspective mode
我已经成功地使用正交相机投射和绘制调试光线,但我将其更改为透视并且我的 none 光线似乎不再起作用(在场景视图中,我的调试光线不起作用用我的鼠标移动)。
这是我的正交代码,我需要做哪些不同的透视?
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 模式。
我想直接从文档中得到答案
http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
首先我们需要知道 DrawRay 函数期望的参数是什么
它想要光线的原点,方向和距离(你也可以给它一个颜色,和其他参数)。
public static void DrawRay(Vector3 start, Vector3 dir, Color color = Color.white, float duration = 0.0f, bool depthTest = true);
所以现在我们需要知道光线原点和 ray.point 位置之间的方向,为了找到我们可以使用减法...
If one point in space is subtracted from another, then the result is a
vector that “points” from one object to the other:
// 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.
这将是生成的代码...
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);
}
https://docs.unity3d.com/Manual/DirectionDistanceFromOneObjectToAnother.html
https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html
我已经成功地使用正交相机投射和绘制调试光线,但我将其更改为透视并且我的 none 光线似乎不再起作用(在场景视图中,我的调试光线不起作用用我的鼠标移动)。
这是我的正交代码,我需要做哪些不同的透视?
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 模式。
我想直接从文档中得到答案 http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
首先我们需要知道 DrawRay 函数期望的参数是什么
它想要光线的原点,方向和距离(你也可以给它一个颜色,和其他参数)。
public static void DrawRay(Vector3 start, Vector3 dir, Color color = Color.white, float duration = 0.0f, bool depthTest = true);
所以现在我们需要知道光线原点和 ray.point 位置之间的方向,为了找到我们可以使用减法...
If one point in space is subtracted from another, then the result is a vector that “points” from one object to the other:
// 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.
这将是生成的代码...
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);
}
https://docs.unity3d.com/Manual/DirectionDistanceFromOneObjectToAnother.html https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html