如何在另一组对象之后实例化一个Object并将其置于其间

时间:2018-01-09 18:50:09

标签: c# unity3d

我试图在一组对象移动一定距离后实例化一个对象。在我的代码中,我正在制作墙壁,而我想要做的就是创建两个杆,拖动我的鼠标并在两极之间创建一个墙,这就是我所拥有的。我想要遵循的是,如果两个极之间的距离超过第一个墙支撑并且添加到一个指数中,并且新的墙在第一个墙和末端极之间实例化。

void adjustWall()
{
    //start and end pole look at each other
    startPole.transform.LookAt(endPole.transform.position);
    endPole.transform.LookAt(startPole.transform.position);
    //gets the distance between the start and end poles
    float distance = Vector3.Distance(startPole.transform.position, endPole.transform.position);
    //places the wall between the 2 poles, rotates it to face both of them and scales it between them.
    wall.transform.position = startPole.transform.position + distance / 2 * startPole.transform.forward;
    wall.transform.rotation = endPole.transform.rotation;
    //wall.transform.localScale = new Vector3(wall.transform.localScale.x, wall.transform.localScale.y, distance); 
    if (distance > 10)
    {
        //if the distance is greater than 10, set wall, add it to an index then instaniate a new wall between that wall and the end poll. Do this Once until it's time to create a new wall.
    }
}
在我的Update函数中调用

adjustWall。

1 个答案:

答案 0 :(得分:0)

在运行时实例化预制件 https://docs.unity3d.com/Manual/InstantiatingPrefabs.html

public GameObject prefab;

// ...

if (distance > 10)
{
    // Calculate your target position
    Vector3 pos;

    // Instantiate the wall's GO
    var wall = Instantiate(prefab, pos, Quaternion.identity) as GameObject;
}
相关问题