Unity旋转对象,从数组中实现

时间:2017-04-05 08:21:13

标签: c# unity3d rotation

任何人都可以帮助我,这是我最后的项目。我已经制作了可以从数组存储中生成对象的脚本,然后我想应用一些轮换你们可以告诉我我做了什么吗? (P.S我在下面附上了我的剧本,所以希望你们看一下)

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

public class spawn : MonoBehaviour {
    //public string[] objek;
            int buatrandom;
            int jumlahrandom = 16 ;
            int objek1 ;
            int objek2 ;
            int objek3 ;
            public string objname1;
            public string objname2;
            public string objname3;
            public GameObject target1;
            public GameObject target2;
            public GameObject target3;
            public int [] simpannomorobject ;
            public GameObject[] nomorasset;
            public float speed =10f;
        // Use this for initialization
        void Start () {
            simpannomorobject = new int[3]; 
            for (int i = 0; i < 2; i++) {
                buatrandom = Random.Range (0, jumlahrandom);
                simpannomorobject [i] = buatrandom;
        if (i > 0){
            if (i < 3) {
                buatobjek ();
            }
        }
    }

}

void buatobjek (){
    objek1 = simpannomorobject [0];
    objek2 = simpannomorobject [1];
    objek3 = simpannomorobject [2];
    Instantiate (nomorasset [objek1], new Vector3 (0, 0, 3.0f), Quaternion.identity);
    Instantiate (nomorasset [objek2], new Vector3 (4.0f, 0, 0), Quaternion.Euler(0,90,0));
    Instantiate (nomorasset [objek3], new Vector3 (-4.0f, 0, 0), Quaternion.Euler(0,-90,0));
    objname1 = nomorasset [objek1].name;
    objname2 = nomorasset [objek2].name;
    objname3 = nomorasset [objek3].name;
}

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

}
}

2 个答案:

答案 0 :(得分:1)

您可以做的是将它们保存在变量中。

GameObject myObject = new GameObject();    
myObject = Instantiate (nomorasset [objek1], new Vector3 (0, 0, 3.0f), Quaternion.identity);

然后您可以使用myObject作为对实例化GameObject的引用

然后对于旋转部件,您可以使用Transform.Rotate()

来自unity scripting API

using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        // Rotate the object around its local X axis at 1 degree per second
        transform.Rotate(Vector3.right * Time.deltaTime);

        // ...also rotate around the World's Y axis
        transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
    }
}

然后您可以使用以下方法对您刚刚实例化的对象进行转换:myObject.transform.Rotate(Vector3.right * Time.deltaTime);

答案 1 :(得分:1)

要引用您实例化的对象,而不是:

Instantiate (nomorasset [objek1], new Vector3 (0, 0, 3.0f), Quaternion.identity);

只需写下:

GameObject yourObject = Instantiate (nomorasset [objek1], new Vector3 (0, 0, 3.0f), Quaternion.identity);

然后,旋转对象:

// Rotate the object around its local X axis at 1 degree per second
yourObject.transform.Rotate(Vector3.right * Time.deltaTime);

有关轮播的详情,请访问unity documantation

相关问题