【问题标题】:Unity rotating object that instatiated from array从数组实例化的 Unity 旋转对象
【发布时间】:2017-04-05 08:21:13
【问题描述】:

谁能帮助我,这是我的最后一个项目。我已经制作了可以从数组存储中生成对象的脚本,然后我想应用一些旋转,你们能告诉我我应该做什么吗? (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 () {

}
}

【问题讨论】:

  • 问题到底是什么?
  • 使用统一@Hristo脚本制作旋转动画

标签: c# unity3d rotation


【解决方案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);

【讨论】:

  • 好吧,我试试看,旋转脚本是不是这样的myObject.transform.Rotate(Vector3.right *Time.DeltaTime);@Shogunivar
  • 是的,你必须把它放在更新中,所以它会触发每个更新的帧
  • 那么我把 GameObject myObject = new GameObject(); 放在哪里?我应该把它放在 start 中还是像 public object 一样? @Shogunivar
  • 创建一个像public GameObject myObject;这样的公共对象,然后把myObject = new GameObject();放在start()中
  • 谢谢老兄,你摇滚,是的,非常感谢@Shogunivar
【解决方案2】:

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

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

【讨论】:

  • 感谢您的帮助,我也会尝试您的建议@italy_421
猜你喜欢
  • 1970-01-01
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
相关资源
最近更新 更多