Author Topic: PoolManager 2 spawn from vector position  (Read 1928 times)


  • Administrator
  • Hero Member
  • *****
  • Posts: 15398
  • Official Playmaker Support
    • View Profile
PoolManager 2 spawn from vector position
« on: December 17, 2012, 09:33:56 AM »

 Following a request, please find a new version of the Pool Manager 2 spawn action that now accepts a vector3 to define the spawning position, if a gameObject is also define, both are added, so you can offset from a gameObject position this way too.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
[ActionCategory("Path-o-logical/Pool Manager 2")]
[Tooltip("Use Spawn() instead of Unity's Instantiate(). The function signature is the same but the return type is different and Spawn() will use an instance from the pool if one is available.")]
public class PmtSpawn : FsmStateAction
[Tooltip("Pool name")]
public FsmString poolName;

[Tooltip("GameObject or prefab to spawn")]
public FsmGameObject gameObject;

[Tooltip("Use this to define the transform of the spawned GameObject when created")]
public FsmGameObject spawnTransform;

[Tooltip("OR a world position, If spawnTransform is declared, OrSpawnPosition will be added to the transform position for offset")]
public FsmVector3 OrSpawnPosition;

[Tooltip("Store the returned the spawned GameObject.")]
public FsmGameObject spawnedGameObject;

[Tooltip("Event triggered if spawn was successful")]
public FsmEvent successEvent;

[Tooltip("Event triggered if spawn was not successful. It fails if the 'limit' option was used and the limit was reached.")]
public FsmEvent failEvent;

public override void Reset()
poolName = null;
gameObject = null;
spawnTransform = null;
OrSpawnPosition = null;

spawnedGameObject = null;
successEvent = null;
failEvent = null;

public override void OnEnter()

void DoSpawn()
if (poolName.Value == "")

if (gameObject.Value == null)

Vector3 pos =;
Quaternion quat =  Quaternion.identity;

GameObject go = spawnTransform.Value;

if (go !=null){
pos = go.transform.position;
quat = go.transform.rotation;

if (OrSpawnPosition!=null)
pos += OrSpawnPosition.Value;

Transform result = PoolManager.Pools[poolName.Value].Spawn(gameObject.Value.transform,pos,quat);

if ( result != null){
spawnedGameObject.Value = result.gameObject;