Unity:如何在运行时获得y轴生成的zigzag路径

时间:2016-07-28 05:19:07

标签: unity3d unityscript unity5

我正在制作像Hilclimb这样的亚军游戏,但我不知道如何在之字形路径表面生成硬币。 这是我的道路 enter image description here

它是随机生成的。但是如何在运行时获得路径中的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));     


    }
}

1 个答案:

答案 0 :(得分:1)

如果您无法访问路径上的确切点,但是您使用的是碰撞器近似路径,那么您可以使用RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301] RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} !^m\. [NC] RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] Physics.Raycast来确定y坐标给定x的路径(如果你在3D空间z中)。

给定x(假设您在2D空间中),您可以按以下步骤操作:
1.选择保证低于/高于您的路径的y值 2.如果您为上面的y选择了一个值,请从方向Physics2D.Raycast的{​​{1}}开始执行Physics.Raycast。您还可以使用图层蒙版来确保您只能击中路径的对撞机 3.命中点的y分量是您要查找的路径的y值