通过实例化对象上的脚本旋转对象 [英] Rotate object through script on Instantiate object
问题描述
我正在通过脚本在特定坐标(x,y,z)实例化对象,然后我想在特定坐标(x,y,z)处旋转对象. 这是我的代码,但是没有旋转
I am Instantiating object through script at specific coordinates(x,y,z), and after that i want to rotate it at specific coordinates(x,y,z). here is my code but it doesn't rotating
using UnityEngine;
using System.Collections;
public class Generator : MonoBehaviour {
public GameObject fire;
//public GameObject firetaps;
//System.Random randomize = new System.Random();
// Use this for initialization
void Start () {
//int fun=randomize.Next(1,6);
switch (1) {
case 1:
PlaceFire();
break;
/*case 2:
PlaceFire1 ();
break;
case 3:
PlaceFire2 ();
break;*/
}
}
void PlaceFire()
{
Instantiate(fire, GeneratedPosition(), Quaternion.identity);
fire.transform.eulerAngles = new Vector3 (275.0941f,287.0612f,72.63131f);// it does not rotate :-(
//fire.transform.Rotate(275.0941f,287.0612f,72.63131f);
}
Vector3 GeneratedPosition()
{
float x,y,z;
x = -142.5f;
y = 39.165f;
z =9.9817f;
return new Vector3(x,y,z);
}
推荐答案
您需要将Instantiate的返回值存储在您的fire字段中.您从未旋转实例化的游戏对象. (另请阅读修改内容!)
You would need to store the return value of Instantiate in your fire field. You never rotated the instantiated gameobject. (also read the edit!!)
Instantiate(fire, GeneratedPosition(), Quaternion.identity);
fire.transform.eulerAngles = new Vector3(275.0941f,287.0612f,72.63131f);
应该是
fire = Instantiate(fire, GeneratedPosition(), Quaternion.identity) as GameObject;
fire.transform.eulerAngles = new Vector3(275.0941f,287.0612f,72.63131f);
你的魔法值也是奇怪的.
also your magic values are odd.
修改 我只是意识到火是你的预制件.因此,您可能要使用局部变量.
edit I just realized fire is your prefab. So you might want to use a local variable instead.
GameObject fireInstance = Instantiate(fire, GeneratedPosition(), Quaternion.identity) as GameObject;
fireInstance.transform.eulerAngles = new Vector3(275.0941f,287.0612f,72.63131f);
Instantiate重载的第三个参数已经旋转了,那么为什么不立即将其传递到那里呢?
Also the 3rd argument of this overload of Instantiate takes a rotation already, so why not pass it there right away?
Instantiate(fire, GeneratedPosition(), Qaternion.Euler(275.0941f,287.0612f,72.63131f));
Idk是否仍然需要参考,则视情况而定.但是请注意,它仅返回需要转换为所需类型的对象.
Idk if you would still need a reference then, depends. But note how it only returns object you will need to cast to the desired type.
这篇关于通过实例化对象上的脚本旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!