【问题标题】:Making two objects distance time dependent and not range dependant使两个对象的距离依赖于时间而不依赖于范围
【发布时间】:2016-05-05 23:17:38
【问题描述】:

我正在尝试创建一个游戏,您可以在其中爬上墙壁,障碍物会朝您而来:

我使用以下代码使障碍物移动工作:

public class ObstacleSpawn : MonoBehaviour {

    public PlayerScript pScript;
    public ObstacleScript oScript;

    public GameObject player;
    public GameObject obstacle;

    public float randomSpawnMin;
    public float randomSpawnMax;

    // Use this for initialization
    void Start () {
        InvokeRepeating ("Spawn", 2F, Random.Range (randomSpawnMin, randomSpawnMax));
    }

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

    }

    void Spawn() {
        if (pScript.isRight == true && pScript.inAir == false) {
            obstacle.transform.localScale = new Vector3 (-1, 1, 1);
            Instantiate (obstacle, player.transform.position + new Vector3 (0.05F, (4F + pScript.playerDimensionY + oScript.obstacleDimensionY) * Time.deltaTime, 0), Quaternion.identity);
        } else if (pScript.isRight == false && pScript.inAir == false) {
            obstacle.transform.localScale = new Vector3 (1, 1, 1);
            Instantiate (obstacle, player.transform.position + new Vector3 (-0.05F, (4F + pScript.playerDimensionY + oScript.obstacleDimensionY) * Time.deltaTime, 0), Quaternion.identity);
        }
    }
}

可以看出,我使用 Instantiate 来决定我的障碍物在哪里产生。目前它设置为在玩家位置 + 我的 Vector3 处生成。对于我的 Y 轴,我决定放入 4F + playerDimensionY 和 barrierDimensionY,它们是我的精灵大小的一半,所以距离是从障碍物底部到玩家顶部,而不是精灵原点。

这可以正常工作,但我创建这个游戏的目的是为了进行反应测试。我需要障碍物来产生固定的秒数。 1 秒后,0.5 秒后等。

我如何实现这一目标?我尝试测量长度与它向下移动的速度相比,但没有成功。它目前移动了 4 个 Unity 单位。

亲切的问候

【问题讨论】:

    标签: c#


    【解决方案1】:

    为了让你朝着正确的方向前进:(将此添加到障碍物的更新功能中):

    var seconds = 5f; // <- 5 seconds to reach the bottom of the screen.
    var screenheight = ?? ( depends if you are using NGUI, Sprites, or wharever, I suggest using SCreen.height and fitting the size of the objects to fit that ) 
    var speed = new Vector3( 0f, -screenheight*Time.deltaTime*seconds, 0f ); // now it takes 5 seconds to reach the position of the player if  he is sitting at the bottom.
    obstacles.transform.position = obstacles.transform.position + speed;
    

    【讨论】:

    • 我试着用你的答案运行游戏。它并没有改变任何东西。它基本上在玩家身上产生。我应该在我的实例化中的某处添加障碍.transform.position 吗?
    • 您需要将其添加到 ObstacleScript 并在生成对象时将初始化秒数设置为 sometihng。
    • 抱歉我缺乏知识,但如果我需要初始化秒,不应该要求它是一个全局变量,而不是在 Update() 函数中吗?还是我误解了初始化的意思?
    • 是的,如果您想将 ti 配置为 5 以外的其他值,则需要将其作为公共变量放入 ObstacleScript 中。
    • 在尝试了一些事情之后,我不相信你的建议有效。我当前的屏幕高度是 800 像素。将它与我的 Y 轴 deltaTime 相乘,三角形向南移动的速度只是乘以 800,产生了一个疯狂的无法使用的速度。我是不是哪里出了问题?
    猜你喜欢
    • 2015-03-09
    • 2017-08-14
    • 2020-04-06
    • 2014-12-09
    • 2018-09-21
    • 2019-02-02
    • 2019-08-23
    • 2018-02-27
    相关资源
    最近更新 更多