我无法在代码中看到问题,但在游戏中如果镜头前面的任何地方都有镜子,则忽略所有其他对象
请有人帮助我,为什么会这样。
我不能这样,因为“这是所有代码和东西”。
public class LaserBeam : MonoBehaviour {
LineRenderer lr;
public bool isOpen = true;
Vector3 s;
void Start () {
lr = GetComponent<LineRenderer>();
}
void Update () {
s = new Vector3(transform.position.x, transform.position.y + (2 / 5f), transform.position.z);
lr.SetPosition(0, s);
lr.SetWidth(0.3f, 0.3f);
if (isOpen)
{
RaycastHit[] Hit = Physics.RaycastAll(s, transform.forward, 100.0F);
//Debug.Log("isOpen W");
if (Hit.Length > 0)
{
for (int x = 0; x < Hit.Length; x++)
{
Debug.Log(Hit[x].collider.tag + " ID: " + x);
if (Hit[x].collider.tag == "Mirror" || !Hit[x].collider.isTrigger)
{
Debug.DrawLine(s, Hit[x].point, Color.blue);
lr.SetPosition(1, Hit[x].point);
// Debug.Log("loop W" + x);
if (Hit[x].collider.tag == "Mirror") Reflect(s, Hit[x], 0);
else lr.SetVertexCount(2);
break;
}
else if (x == Hit.Length - 1)
{
lr.SetVertexCount(2);
Debug.DrawLine(s, transform.forward * Int16.MaxValue, Color.blue);
lr.SetPosition(1, transform.forward * Int16.MaxValue);
break;
}
}
}
else
{
lr.SetVertexCount(2);
Debug.DrawLine(s, transform.forward * Int16.MaxValue, Color.blue);
lr.SetPosition(1, transform.forward * Int16.MaxValue);
}
}
else
{
lr.SetVertexCount(2);
lr.SetPosition(1, s);
}
}
public void Reflect(Vector3 start, RaycastHit hit, int id)
{
lr.SetVertexCount(id + 3);
Vector3 p = Vector3.Reflect(hit.point - start, hit.normal);
Debug.DrawRay(hit.point, hit.normal * 3);
Debug.DrawLine(hit.point, p + hit.point, Color.blue);
RaycastHit[] Hit1 = Physics.RaycastAll(hit.point, p, 100.0F);
if (Hit1.Length > 0)
{
for (int x = 0; x < Hit1.Length; x++)
{
if (Hit1[x].collider.tag == "Mirror" || !Hit1[x].collider.isTrigger)
{
Debug.DrawLine(hit.point, Hit1[x].point, Color.blue);
//Debug.DrawLine(hit.point, Hit[x].point, Color.blue);
//lr.SetPosition(id + 1,(hit.point + start) / 2);
//lr.SetPosition(id + 2, hit.point);
lr.SetPosition(id + 2, Hit1[x].point);
if (Hit1[x].collider.tag == "Mirror")
{
Reflect(hit.point, Hit1[x], (id + 1));
return;
}
else lr.SetVertexCount(id + 3);
return;
}
else if (x == Hit1.Length - 1)
{
lr.SetVertexCount(id + 3);
Debug.DrawLine(hit.point, Vector3.Normalize(p) * Int16.MaxValue, Color.blue);
//lr.SetPosition(id + 1, (hit.point + start) / 2);
//lr.SetPosition(id + 2, hit.point);
lr.SetPosition(id + 2, Vector3.Normalize(p) * Int16.MaxValue);
return;
}
return;
}
}
else
{
lr.SetVertexCount(id + 3);
Debug.DrawLine(hit.point, Vector3.Normalize(p) * Int16.MaxValue, Color.blue);
//lr.SetPosition(id + 1, (hit.point + start) / 2);
//lr.SetPosition(id + 2, hit.point);
lr.SetPosition(id + 2, Vector3.Normalize(p) * Int16.MaxValue);
return;
}
// Debug.Log(id);
}
}
我在代码中看不到问题,但在游戏中如果镜子前面的任何地方都有镜子,则忽略所有其他对象
请有人帮助我,为什么会这样。
我不能这样,因为“这是所有代码和东西”。
答案 0 :(得分:1)
我不认为你可以在这种情况下逃脱图层,但你需要知道如何使用它们,无论如何,这应该有用:
if (Hit.Length > 0)
{
float firstHitDistance = 100000f; //or something ridiculously high
RaycastHit firstHit;
for (int x = 0; x < Hit.Length; x++)
{
if(Hit[x].distance < firstHitDistance){
firstHitDistance = Hit[x].distance;
firstHit = Hit[x];
}
}
if (firstHit.collider.tag == "Mirror" || !firstHit.collider.isTrigger)
{
Debug.DrawLine(s, firstHit.point, Color.blue);
lr.SetPosition(1, firstHit.point);
// Debug.Log("loop W" + x);
if (firstHit.collider.tag == "Mirror") Reflect(s, , 0);
else lr.SetVertexCount(2);
break;
}
}
抱歉格式错误,我在Windows上:/