【发布时间】:2015-10-01 01:01:30
【问题描述】:
我目前在使我的线渲染器完全按照我的光线投射时遇到问题。 here 是我找到的反射光线投射的来源 这是我的代码:
/clamp the number of reflections between 1 and int capacity
nReflections = Mathf.Clamp(nReflections,1,nReflections);
//cast a new ray forward, from the current attached game object position
ray = new Ray(goTransform.position,goTransform.right);
// Debug.DrawRay(hit.point, inDirection*100, Color.magenta);
//represent the ray using a line that can only be viewed at the scene tab
Debug.DrawRay(goTransform.position,goTransform.right * 100, Color.magenta);
//set the number of points to be the same as the number of reflections
nPoints = nReflections;
//make the lineRenderer have nPoints
lineRenderer.SetVertexCount(nPoints);
//Set the first point of the line at the current attached game object position
lineRenderer.SetPosition(0,goTransform.position);
for(int i=0;i<=nReflections;i++)
{
//If the ray hasn't reflected yet
if(i==0)
{
//Check if the ray has hit something
if(Physics.Raycast(ray.origin,ray.direction, out hit, 100)&& ( hit.transform.name != "Ceiling"))//cast the ray 100 units at the specified direction
{
//the reflection direction is the reflection of the current ray direction flipped at the hit normal
inDirection = Vector3.Reflect(ray.direction,hit.normal);
ray = new Ray(hit.point,inDirection);
//cast the reflected ray, using the hit point as the origin and the reflected direction as the direction
// nReflections = nReflections - 1;
Debug.DrawRay(hit.point, hit.normal*3, Color.blue);
//represent the ray using a line that can only be viewed at the scene tab
//Draw the normal - can only be seen at the Scene tab, for debugging purposes
//Print the name of the object the cast ray has hit, at the console
Debug.DrawRay(hit.point, inDirection*100, Color.magenta);
// if (hit.transform.tag){
//Debug.Log("Object name: " + hit2d.transform.tag);
//
// }
//if the number of reflections is set to 1
if(nReflections==1)
{
//add a new vertex to the line renderer
lineRenderer.SetVertexCount(++nPoints);
}
//set the position of the next vertex at the line renderer to be the same as the hit point
lineRenderer.SetPosition(i+1,hit.point);
// lineRenderer.SetWidth(0,1);
} else {
if(nReflections==1)
{
//add a new vertex to the line renderer
lineRenderer.SetVertexCount(++nPoints);
}
lineRenderer.SetPosition(i+1,hit.point);
}
}
else // the ray has reflected at least once
{
//Check if the ray has hit something
if(Physics.Raycast(ray.origin,ray.direction, out hit, 100)&& ( hit.transform.name != "Ceiling"))//cast the ray 100 units at the specified direction
{
//the refletion direction is the reflection of the ray's direction at the hit normal
inDirection = Vector3.Reflect(inDirection,hit.normal);
ray = new Ray(hit.point,inDirection);
Debug.DrawRay(hit.point, inDirection*100, Color.magenta);
//Print the name of the object the cast ray has hit, at the console
// Debug.Log("Object name: " + hit.transform.tag);
//add a new vertex to the line renderer
lineRenderer.SetVertexCount(++nPoints);
//set the position of the next vertex at the line renderer to be the same as the hit point
lineRenderer.SetPosition(i+1,hit.point);
// lineRenderer.SetWidth(0,1);
} else {
lineRenderer.SetVertexCount(++nPoints);
lineRenderer.SetPosition(i+1,hit.point);
}
}
}
对于第一次迭代,该行与我想要的行匹配,但是当我将 1 作为我的反射数时,它返回给我 2 个反射。当我输入 2 时,它返回给我 3 个反射。当我放 3 个或更多时,它仍然做同样的事情,但这次它给了我多余的线点,而多余的给了我一个未知的位置。
【问题讨论】: