2016-08-30 13 views
-1

我想要它做的是在編輯器和運行遊戲時看到所有的球體在同一時間移動到另一個方向,或者它將是相同的方向直到endPoint,然後回到循環中的startPoint沒有停止。如何將地形上的所有球體從原始位置移動到新位置,然後回到原來的循環中?

這是我的腳本:

using System; 
using UnityEngine; 
using Random = UnityEngine.Random; 

[ExecuteInEditMode] 
public class SphereBuilder : MonoBehaviour 
{ 
    // for tracking properties change 
    private Vector3 _extents; 
    private int _sphereCount; 
    private float _sphereSize; 

    /// <summary> 
    ///  How far to place spheres randomly. 
    /// </summary> 
    public Vector3 Extents; 

    /// <summary> 
    ///  How many spheres wanted. 
    /// </summary> 
    public int SphereCount; 

    public float SphereSize; 

    private void OnValidate() 
    { 
     // prevent wrong values to be entered 
     Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z)); 
     SphereCount = Mathf.Max(0, SphereCount); 
     SphereSize = Mathf.Max(0.0f, SphereSize); 
    } 

    private void Reset() 
    { 
     Extents = new Vector3(250.0f, 20.0f, 250.0f); 
     SphereCount = 100; 
     SphereSize = 20.0f; 
    } 

    private void Update() 
    { 
     UpdateSpheres(); 
    } 

    private void UpdateSpheres() 
    { 
     if (Extents == _extents && SphereCount == _sphereCount && Mathf.Approximately(SphereSize, _sphereSize)) 
      return; 

     // cleanup 
     var spheres = GameObject.FindGameObjectsWithTag("Sphere"); 
     foreach (var t in spheres) 
     { 
      if (Application.isEditor) 
      { 
       DestroyImmediate (t); 
      } 
      else 
      { 
       Destroy(t); 
      } 
     } 

     var withTag = GameObject.FindWithTag("Terrain"); 
     if (withTag == null) 
      throw new InvalidOperationException("Terrain not found"); 

     for (var i = 0; i < SphereCount; i++) 
     { 
      var o = GameObject.CreatePrimitive(PrimitiveType.Sphere); 
      o.tag = "Sphere"; 
      o.transform.localScale = new Vector3(SphereSize, SphereSize, SphereSize); 

      // get random position 
      var x = Random.Range(-Extents.x, Extents.x); 
      var y = Extents.y; // sphere altitude relative to terrain below 
      var z = Random.Range(-Extents.z, Extents.z); 

      // now send a ray down terrain to adjust Y according terrain below 
      var height = 10000.0f; // should be higher than highest terrain altitude 
      var origin = new Vector3(x, height, z); 
      var ray = new Ray(origin, Vector3.down); 
      RaycastHit hit; 
      var maxDistance = 20000.0f; 
      var nameToLayer = LayerMask.NameToLayer("Terrain"); 
      var layerMask = 1 << nameToLayer; 
      if (Physics.Raycast(ray, out hit, maxDistance, layerMask)) 
      { 
       var distance = hit.distance; 
       y = height - distance + y; // adjust 
      } 
      else 
      { 
       Debug.LogWarning("Terrain not hit, using default height !"); 
      } 

      // place ! 
      o.transform.position = new Vector3(x, y, z); 
     } 

     _extents = Extents; 
     _sphereCount = SphereCount; 
     _sphereSize = SphereSize; 
    } 
} 

更新

我創造了另一個C#腳本,並將其拖動到地形。

using UnityEngine; 
using System.Collections; 

public class MoveSpheres : MonoBehaviour { 

    public Transform _transform; 
    private Vector3 pos1 = new Vector3(-4,0,0); 
    private Vector3 pos2 = new Vector3(4,0,0); 
    public float speed = 1.0f; 
    // Use this for initialization 
    void Start() { 



    } 

    // Update is called once per frame 
    void Update() { 

     _transform.position = Vector3.Lerp (pos1, pos2, Mathf.PingPong(Time.time*speed, 1.0f)); 


    } 
} 

此腳本移動一個球體。但是我想移動所有的球體。 而且,如果我在第一個腳本中更改了球體的數量,那麼它們也會移動它們。

問題出在第一個腳本中的Destroy部分。

+0

比原來的問題,其他還有幾個代碼味道要擺脫:是''ExecuteInEditMode''真的有必要嗎?爲什麼要在添加公共變量時搜索所有對象(甚至是基於它們的根名稱)?你可以獲得所有的對象轉換,而不是整個對象(也就是說,它幾乎沒有區別)。此外,您可能希望在對象上附加移動的腳本,而不是在一個循環中進行所有變換(單聲道/單元編譯器優化的更好 - 也可以在一次更新中獲得性能問題,並且循環時間很長,一個obj –

回答

0

可以線性插值從一個位置球到另一個和背部https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

+0

它的工作,但只有當我刪除不使用在另一個腳本的銷燬。毀滅只是destry的球面,並使其在新的腳本丟失轉換,即使我添加一個新的gameobject球面,沒有連接到我的其他腳本。 –

+1

好吧,放鬆一下,我們可以解決這個問題,但是!一次只有一件事,我希望你停止摧毀球體。告訴我你什麼時候想要摧毀球體?當它移動或停止移動時 – LumbusterTick

+0

對不起。只有當我改變球體計數時,我纔想要摧毀球體,例如,如果我將球體數量改爲1,則摧毀其他球體並創建1個新球體;如果我設置了10秒球摧毀1並創建新的10個球體。這是線路DestroyImmediate(t);在我的腳本中做。但我被告知使用Destroy而不是DestroyImmediate,但是Destroy並沒有達到我想要的。 –

相關問題