【发布时间】:2018-06-28 15:13:13
【问题描述】:
我想在项目符号对象创建后旋转它(即从池中取出)。
这是我的函数,子弹从对象池中取出并分别定位:
void CreateBullet (GameObject bulletObj, Transform _bulletPos, float xVal = 0f, float yVal = 0f) {
//get bullet object from pool
bulletObj = bulletPool.GetInstance (playerShotsGO);
bulletObj.transform.position = _bulletPos.position;
Vector2 pos = bulletPos.transform.position;//Position 0,0
pos.x += xVal;
pos.y += yVal;
bulletObj.transform.position = pos;
bulletObj.transform.SetParent (playerShotsGO.transform, true);
}
这是我的 PlayerBullet 脚本的更新函数,用于计算子弹的位置:
void Update () {
Vector2 position = transform.position;
//compute the bullet's new position
position = new Vector2 (position.x, position.y + speed * Time.deltaTime);
//update the bullet's position
transform.position = position;
}
这里我的对象被实例化了:
protected virtual GameObject AllocateInstance (bool parent, GameObject parentObj) {
GameObject instance = (GameObject)GameObject.Instantiate (prefab);
if (parent) {
instance.transform.SetParent(parentObj.transform, true);
}
instance.SetActive(false);
pool.Add(instance);
return instance;
}
【问题讨论】:
-
您在场景中的何处实例化您的对象?通常,您会创建游戏对象,或将其设置为活动对象,然后更改变换的旋转
-
我更新了我的问题。