【问题标题】:Platform Positions Randomly Generated - Not Generating within possible range平台位置随机生成 - 不在可能范围内生成
【发布时间】:2021-11-18 13:29:45
【问题描述】:

基本上,我遇到的问题是平台生成垂直,但是它们会移到右上角(见图)。因此玩家无法跳到平台,而我必须提高他们的速度和跳跃值。我想让平台可以在随机范围内生成,也可以跳跃。

平台生成代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformGenerator : MonoBehaviour
{
    public GameObject thePlatform;
    public Transform generationPoint;
    public float distanceBetween;

    private float platformWidth;


    public float distanceBetweenMin;
    public float distanceBetweenMax;

    // Start is called before the first frame update
    void Start()
    {
        platformWidth = thePlatform.GetComponent<BoxCollider2D>().size.x;
    }

    // Update is called once per frame
    void Update()
    {
        if(transform.position.y < generationPoint.position.y)
        {
            distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);

            transform.position = new Vector3(transform.position.y + platformWidth + distanceBetween, transform.position.x, transform.position.z);

            Instantiate(thePlatform, transform.position, transform.rotation);
        }
    }
}

平台破坏代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformDestroyer : MonoBehaviour
{
    public GameObject platformDestructionPoint;

    // Start is called before the first frame update
    void Start()
    {
        platformDestructionPoint = GameObject.Find("PlatformDestructionPoint");
    }

    // Update is called once per frame
    void Update()
    {
        if(transform.position.x < platformDestructionPoint.transform.position.x)
        {
            Destroy(gameObject);
        }
    }
}

这是我正在经历的图像。

【问题讨论】:

    标签: c# unity3d random


    【解决方案1】:

    看起来像在

     transform.position = new Vector3(transform.position.y + platformWidth + distanceBetween, transform.position.x, transform.position.z);
    

    你翻转了 X 和 Y,它可能应该是

     transform.position = new Vector3(transform.position.x + platformWidth + distanceBetween, transform.position.y, transform.position.z);
    

    顺便说一句,你也可以简单地写成

    transform.position += new Vector2(platformWidth + distanceBetween, 0);
    

    或者如果您实际上更希望它们也是垂直的(如楼梯),您可能更愿意表示类似的东西,例如

    transform.position += new Vector2(platformWidth, distanceBetween);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多