2016-01-20 203 views
2

我想繪製一條線從一個對象到目標對象。我已成功地做到這一點,不過線條從中心從平面邊緣繪製線Unity

抽我想什麼做的是汲取一個平面的邊緣線到面的邊緣上的目標

screenshot

在此圖像中的白線是目前繪製連接,紅色線是怎麼想的線要繪製

這是線的繪製,現在

foreach (GameObject planet in LinkedPlanets) 
    { 
     GameObject PlanetLine = new GameObject(); 
     PlanetLine.transform.position = this.transform.position; 
     PlanetLine.name = this.transform.name + " To " + planet.transform.name; 
     PlanetLine.transform.parent = this.transform; 
     PlanetLine.AddComponent<LineRenderer>(); 
     LineRenderer lr = PlanetLine.GetComponent<LineRenderer>(); 
     lr.material = new Material(Shader.Find("Particles/Additive")); 
     lr.SetWidth(3f, 3f); 
     lr.SetPosition(0, this.transform.position); 
     lr.SetPosition(1, planet.transform.position); 
     lr.GetComponent<Renderer>().material.SetColor("_TintColor", new Color(1, 1, 1, 0.2f)); 
} 

回答

1

此代碼未經測試,因此可能是錯誤的。

編輯:

我知道你想從平面的邊緣連接,因此所有變化,而不必從一個星球到另一個方向向量,是在此基礎上載體的長軸有方向。如果「x」更長,則「z」使用Vector3.right乘以一元運算符,這取決於方向矢量是否低於零。編輯

最後你需要知道每個圓圈的半徑。假設在這個例子中你知道它,它是5f和4f。那麼你需要計算「會議」點數。你可以這樣做:

foreach (GameObject planet in LinkedPlanets) 
    { 
     float radiusA = 5f; 
     float radiusB = 4f; 

     GameObject PlanetLine = new GameObject(); 
     PlanetLine.transform.position = this.transform.position; 
     PlanetLine.name = this.transform.name + " To " + planet.transform.name; 
     PlanetLine.transform.parent = this.transform; 
     PlanetLine.AddComponent<LineRenderer>(); 
     LineRenderer lr = PlanetLine.GetComponent<LineRenderer>(); 
     lr.material = new Material(Shader.Find("Particles/Additive")); 
     lr.SetWidth(3f, 3f); 

     Vector3 pointA = Vector3.ClampMagnitude (planet.transform.position - this.transform.position, radiusA); 

     Vector3 pointB = Vector3.ClampMagnitude (this.transform.position -planet.transform.position, radiusB); 

     lr.SetPosition(0, this.transform.position + pointA); 
     lr.SetPosition(1, planet.transform.position + pointB); 
     lr.GetComponent<Renderer>().material.SetColor("_TintColor", new Color(1, 1, 1, 0.2f)); 
} 

所以我們來解釋一下。

首先,你需要得到這樣的方向向量:

planet.transform.position - this.transform.position 

然後夾緊它的長度結束對半徑長度:

Vector3 pointA = Vector3.ClampMagnitude (planet.transform.position - this.transform.position, radiusA); 

最後這個載體添加到實際工作中的位置:

this.transform.position + pointA 
+0

嗨感謝您的答覆,我已經嘗試過這個解決方案,我認爲它會工作,如果點不需要tou飛機或圓圈。 我將如何得到從飛機的邊緣或圓的邊緣去的線。 飛機和地球周圍的圓是分開的東西,圓是運行時繪製的線渲染和飛機只是一個普通的raycasts 這是你的代碼的結果 http:// imgur .COM/FT8Av1i –