1

我用 Raycast Reflection 和 LineRenderers 尝试了几种不同的方法,但似乎没有一个能正常工作。这个,唯一一个半工作的,只在开始时设置自己,从不为移动的对象更新;并且变得非常不稳定,曲折,轻微地上下起伏并严重扭曲(这还没有折射,不要费心指出这一点)。有任何想法吗?我已经坚持了很长时间。

不按要求工作(例如我正在尝试做的事情,全新的代码是一个可行的选择):

@script RequireComponent(LineRenderer)

private var goTransform:Transform;
private var lineRenderer:LineRenderer;

private var ray:Ray;
private var hit:RaycastHit;
private var inDirection:Vector3;
public var nReflections:int = 2;
private var nPoints:int;


function Awake () 
{
    goTransform = this.GetComponent(Transform);
    lineRenderer = this.GetComponent(LineRenderer);
}

function Update () {
    nReflections = Mathf.Clamp(nReflections,1,nReflections);
    ray = new Ray(goTransform.position,goTransform.forward);

    Debug.DrawRay (goTransform.position,goTransform.forward * 100, Color.magenta);

    nPoints = nReflections;
    lineRenderer.SetVertexCount(nPoints); 
    lineRenderer.SetPosition(0,goTransform.position);

    for(var i:int=0;i<=nReflections;i++) {
       if(i == 0) {
         if(Physics.Raycast(ray.origin,ray.direction, hit, 100)) {  
          if(hit.transform.CompareTag("LaserReflector")) {
              inDirection = Vector3.Reflect(ray.direction, hit.normal);
              ray = new Ray(hit.point,inDirection);

              Debug.DrawRay (hit.point, hit.normal*3, Color.blue);
              Debug.DrawRay (hit.point, inDirection*100, Color.magenta);

              Debug.Log("Object name: " + hit.transform.name);

              if(nReflections==1)
              {
                 lineRenderer.SetVertexCount(++nPoints); 
              }

              lineRenderer.SetPosition(i+1, hit.point);
          } 
         }
       }
       else
       {
         if(Physics.Raycast(ray.origin,ray.direction, hit, 100)) {
          if(hit.transform.CompareTag("LaserReflector")) {
              inDirection = Vector3.Reflect(inDirection, hit.normal);
              ray = new Ray(hit.point, inDirection);

              Debug.DrawRay (hit.point, hit.normal*3, Color.blue);
              Debug.DrawRay (hit.point, inDirection*100, Color.magenta);

              Debug.Log("Object name: " + hit.transform.name);

              lineRenderer.SetVertexCount(++nPoints); 
              lineRenderer.SetPosition(i+1,hit.point);
          } 
         }
       }
    }
}
4

0 回答 0