我想要一條光線的反射,但只在 XZ 軸上,而 Y 軸應該只允許我調整反射的高度,但對反射本身沒有影響;
這是常規反射:
if (Physics.Raycast(ray.origin, ray.direction, out hit, remainingLength))
{
lineRenderer.positionCount = 1;
lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
remainingLength -= Vector3.Distance(ray.origin, hit.point);
//temp_normal.x = ray_new.x;
ray = new Ray(hit.point, Vector3.Reflect(ray.direction, hit.normal));
這是我的嘗試,我嘗試使射線原點的 y 與命中點的 y 相同,希望它會自動給我一個 XZ 反射,可惜無濟于事:
代碼:
var hit_2 = hit ;
//Make a new ray with the same y position as the hit point so it doesn't reflect in y
var ray_2= new Ray(new Vector3(transform.position.x,hit.point.y,transform.position.z), transform.forward);
Physics.Raycast(ray_2.origin, ray_2.direction, out hit_2, remainingLength);
lineRenderer.positionCount = 1;
lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
remainingLength -= Vector3.Distance(ray.origin, hit.point);
//temp_normal.x = ray_new.x;
ray = new Ray(hit.point, Vector3.Reflect(ray_2.direction, hit.normal));
if (hit.collider.tag == "Totem")
{
//lineRenderer.material.color = new Color(0.4f, 0.9f, 0.7f, 1.0f);
break;
}
uj5u.com熱心網友回復:
不確定我是否正確理解了這個問題,但是當我閱讀它時,您只想從命中點反映 XZ 軸并忽略Y
。
所以你可以做到這一點
var previousDirection = ray.direction;
previousDirection.y = 0;
ray = new Ray(hit.point, Vector3.Reflect(previousDirection, hit.normal));
或者如果hit.normal
也不是完全 XZ 對齊另外
var previousDirection = ray.direction;
previousDirection.y = 0;
var xzNormal = hit.normal;
xzNormal.y = 0;
ray = new Ray(hit.point, Vector3.Reflect(previousDirection, xzNormal));
或者你可能會反過來,而是簡單地將結果變平Reflect
var newDirection = Vector3.Reflect(ray.direction, hit.normal);
newDirection.y = 0;
ray = new Ray(hit.point, newDirection);
uj5u.com熱心網友回復:
下面ray_direction
我添加了一條我認為您需要的評論。由于您使用線渲染器繪制的光線直接從立方體中出來,假設那是該立方體的區域 y 方向。
還假設,我認為您希望它與地面平行。
RaycastHit hit;
Vector3 ray_start = transform.position; // this already contains the y position you want.
Vector3 ray_direction = transform.forward; // the local forward of this gameObject.transform
// Vector3.forward; // if you whish the global forward direction
if( Physics.Raycast(ray_start, ray_direction, out hit, remainingLength) )
{
// Do something.
}
Vector3.forward
始終是全域方向,因此Vector3(0,0,1);
實體的Transform.forward
與相同。這意味著,該向量是游戲物件旋轉的旋轉。transform.rotation * Vector3.forward
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/528945.html