我想在创建子弹对象(即从池中取出)后旋转它。
这是我的功能,子弹从对象池中取出并分别放置:
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;
}
答案 0 :(得分:1)
基于Unity3d教程中的https://unity3d.com/learn/tutorials/topics/scripting/object-pooling,您需要在池中搜索准备使用的对象。如果找不到,则生成一个新的。然后,您使用SetActive(true)
进行显示,并通过instance.transform.rotation = <Vector3>
设置旋转角度。