2016-07-28 6 views
0

私はヒルクライムのようなランナーゲームを作成しますが、ジグザグパスの表面にコインを生成する方法はわかりません。 これは私のパスです enter image description hereUnity:実行時にy軸を得る方法ジグザグパス

ランダムに生成されます。しかし、実行時にパス内のyの位置を取得する方法は、ランダムな位置にコインが生成されたときにコインがパスよりも若干時間を下回るためです。

実行時にパスの位置を取得して、表面にコインを生成する方法を教えてください。

パス生成コードはこれまたコインです。

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 
public class TerrainGenerator : MonoBehaviour { 

    public chunk previousChunk; 
    public chunk currentChunk; 
    public chunk nextChunk; 
    public ManagePower power; 
    public GameObject character; 
    public Resize resize; 
    public Coin coin; 
    private PickupResize rise; 
    private pickupPower pick; 
    private PickupCoin pCoin; 
    // Use this for initialization 
    void Start() { 
     rise=FindObjectOfType<PickupResize>(); 
     pick=FindObjectOfType<pickupPower>(); 
     pCoin=FindObjectOfType<PickupCoin>(); 
     float widthprefabs = previousChunk.WidthGroundPrefab; 
     previousChunk.RepositionChunk (new Vector3(-widthprefabs*chunk.CHUNK_SIZE,0,0)); 
     currentChunk.RepositionChunk (Vector3.zero); 
     nextChunk.RepositionChunk (new Vector3(widthprefabs*chunk.CHUNK_SIZE,0,0)); 
     power.RepositionPower (new Vector3(widthprefabs*chunk.CHUNK_SIZE,-0.1f,0)); 
     resize.ResizeSquire (new Vector3(25f,-0.1f,0)); 
     coin.ResetCoin (new Vector3(5f,-0.17f,0)); 
    } 

    // Update is called once per frame 
    void Update() { 
     if(playerReachedNextChunk()){ 
      reassineChunk(); 
     } 
     if (rise.rePick) { 
      resize.ResizeSquire(new Vector3 (rise.distancere, -0.1f, 0)); 
      rise.rePick=false; 
     } 

     if (pick.poPick) { 
      power.RepositionPower (new Vector3 (pick.powerdist, -0.1f, 0)); 
      pick.poPick=false; 
     } 

     if (pCoin.distancere < ObjectScript.distance) { 
      int random = Random.Range (1, 10);   
      pCoin.distancere += random; 
      pCoin.reCoin = true; 

     } 

     if (pCoin.reCoin) { 
      coin.ResetCoin (new Vector3 (pCoin.distancere, -0.1f, 0)); 
      pCoin.reCoin=false; 
     } 

     //print ("ddhdhdhdhhd"+ObjectScript.distance); 

    } 

    private bool playerReachedNextChunk(){ 
     bool playerReachedNextChunk = false; 
     float distance = character.transform.position.x - nextChunk.transform.position.x; 
     //print ("Distance " +distance); 
     if (distance > 0) { 
      playerReachedNextChunk=true; 
     } 

     return playerReachedNextChunk; 
    } 

    private void reassineChunk(){ 
     chunk refToprivesChunk = previousChunk; 
     previousChunk = currentChunk; 
     currentChunk = nextChunk; 
     nextChunk = refToprivesChunk; 

     float xPosition = nextChunk.transform.position.x+3 * nextChunk.WidthGroundPrefab * chunk.CHUNK_SIZE; 
     nextChunk.RepositionChunk (new Vector3(xPosition,0,0));  


    } 
} 
+0

を探しているパスのyの値であるあなたが生成する方法についていくつかのより多くの情報を提供し、あなたのパス – TheDjentleman

+0

を表すことができ、あなたの生成されたパスは、コライダーを持っていますか?見た目の写真を投稿してもいいですか? – Programmer

+0

はいパス上にポリゴンコライダーを追加しました(上記の図を参照) – Destro

答えて

1

あなたのパス上の正確なポイントにアクセスすることはできませんが、あなたがパスを近似衝突型加速器を、使用している場合は、そのy座標指定されたパスのを決定するためにPhysics.RaycastまたはPhysics2D.Raycastを使用することができますがx(あなたが3D空間zにいる場合)。

考えるとX次のように作業を進めることができます(2D空間であると仮定した場合):
1.あなたはyの値を選択する場合は、あなたのパス
2.上/下にあることが保証されているyの値を選択します。それは(x,y)からVector2.downに始まるPhysics.Raycastを実行します。また、レイヤーマスクを使用して、パスのコライダーだけを確認することもできます。
3.ヒットポイントのy成分は、あなたが

関連する問題