预制件不会在给定位置实例化

时间:2014-01-21 12:58:06

标签: unity3d instantiation

标题几乎总结了它。我的敌人预制件只是在Y轴上实例化,但硬币预制件正在实例化我想要的方式,即使它们具有相同的坐标。

import System.Collections.Generic;

var enemyPrefab : GameObject;
var coinPrefab : GameObject;
public var Coinnumber = 1;
public var Coinnumberside : float = 0.0f;

   function OnTriggerEnter(other: Collider)
     {
    if (other.tag == "Trigger" )
    {       
        var StagePosition : Vector3;
        StagePosition.z = 5.69 + Coinnumber;
        StagePosition.x = 0.0 + Coinnumberside;
        StagePosition.y = 0.4;

        Instantiate(coinPrefab, StagePosition, Quaternion.identity);

    Coinnumber = Coinnumber + Random.Range(5,10); 
    Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4);



        var EnemyPosition : Vector3;
        EnemyPosition.z = 5.69 + Coinnumber;
        EnemyPosition.x = 0.0 + Coinnumberside;
        EnemyPosition.y = 0.4;

        Instantiate(enemyPrefab, EnemyPosition, Quaternion.identity);

    Coinnumber = Coinnumber + Random.Range(5,10); 
    Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4);   




    }
 }

2 个答案:

答案 0 :(得分:0)

除非我误解了你,否则你试图在实例化硬币的同一位置创造一个敌人。正如我所看到的,这个问题就是这些问题:

Coinnumber = Coinnumber + Random.Range(5,10); 
Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4);
...
EnemyPosition.z = 5.69 + Coinnumber;
EnemyPosition.x = 0.0 + Coinnumberside;

在设置敌人位置之前,您正在重新计算Coinnumber变量。唯一不变的值是Y坐标,因为您重新计算Z和X.因此,例如,当您第一次运行此脚本时,您的硬币将在(5.69f,0.4f,0f)位置产生,然后enemyPosition将获得随机值对此,所以它可以(例如)(10.69f,0.4f,0.2f)

考虑尝试这个:

var StagePosition : Vector3;
StagePosition.z = 5.69 + Coinnumber;
StagePosition.x = 0.0 + Coinnumberside;
StagePosition.y = 0.4;

Instantiate(coinPrefab, StagePosition, Quaternion.identity);
Instantiate(enemyPrefab, StagePosition, Quaternion.identity);

Coinnumber = Coinnumber + Random.Range(5,10); 
Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4);

这会在与你的硬币相同的位置产生敌人,然后为你的下一枚硬币计算一个新的位置。敌人。但是,如果这不是你想要做的,请告诉我,我会改变我的答案。

答案 1 :(得分:0)

import System.Collections.Generic;

var enemyPrefab : GameObject;
var coinPrefab : GameObject;
public var Coinnumber = 1;
public var Coinnumberside : float = 0.0f;
public var Enemynumber = 1;
public var Enemynumberside : float = 0.0f;

function OnTriggerEnter(other: Collider)
{
    if (other.tag == "Trigger" )
    {       
        var StagePosition : Vector3;
        StagePosition.z = 5.69 + Coinnumber;
        StagePosition.x = 0.0 + Coinnumberside;
        StagePosition.y = 0.4;

        Instantiate(coinPrefab, StagePosition, Quaternion.identity);

        Coinnumber = Coinnumber + Random.Range(5,10); 
        Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4);
    }

    if(other.tag == "Trigger" )
    {
        var EnemyPosition : Vector3;
        EnemyPosition.z = 5.69 + Enemynumber;
        EnemyPosition.x = 0.0 + Enemynumberside;
        EnemyPosition.y = 0.4;

        Instantiate(enemyPrefab, EnemyPosition, Quaternion.identity);

        Enemynumber = Enemynumber + Random.Range(5,10); 
        Enemynumberside = Enemynumberside + Random.Range(0.2,-0.4);

    }
}

我仍然不太清楚为什么在我分开它们之后它才起作用但它确实如此,所以我把它作为答案。感谢所有回答并试图解决我问题的人,我真的很感激。