我在特定坐标(x,y,z)处通过脚本实例化对象,之后我想在特定坐标(x,y,z)处旋转它。 这是我的代码,但它没有旋转
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);
}
答案 0 :(得分:2)
您需要在火场中存储Instantiate的返回值。你永远不会旋转实例化的游戏对象。 (也阅读编辑!!)
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);
你的魔法值也很奇怪。
修改强> 我刚刚意识到火是你的预制件。所以你可能想要使用局部变量。
GameObject fireInstance = Instantiate(fire, GeneratedPosition(), Quaternion.identity) as GameObject;
fireInstance.transform.eulerAngles = new Vector3(275.0941f,287.0612f,72.63131f);
Instantiate的这个重载的第三个参数已经进行了一次旋转,为什么不立即将它传递给那里呢?
Instantiate(fire, GeneratedPosition(), Qaternion.Euler(275.0941f,287.0612f,72.63131f));
如果您仍然需要参考,那么,取决于。但请注意它是如何仅返回您需要转换为所需类型的对象。