added spawner for humanoid
This commit is contained in:
29
Assets/8_Scripts/2_General/Body.cs
Normal file
29
Assets/8_Scripts/2_General/Body.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Pool;
|
||||
|
||||
public class Body : MonoBehaviour
|
||||
{
|
||||
private ObjectPool<Body> m_Pool;
|
||||
public Transform m_Pelvis;
|
||||
public int m_DespawnTimer;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
StartCoroutine(DespawnTimer());
|
||||
}
|
||||
|
||||
|
||||
public void SetPool(ObjectPool<Body> pool)
|
||||
{
|
||||
m_Pool = pool;
|
||||
}
|
||||
|
||||
IEnumerator DespawnTimer()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(m_DespawnTimer);
|
||||
m_Pool.Release(this);
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/8_Scripts/2_General/Body.cs.meta
generated
Normal file
11
Assets/8_Scripts/2_General/Body.cs.meta
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 478442ca875dd024ebb476dfc3585dd7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
58
Assets/8_Scripts/2_General/SpawnRB.cs
Normal file
58
Assets/8_Scripts/2_General/SpawnRB.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Pool;
|
||||
|
||||
public class SpawnRB : MonoBehaviour
|
||||
{
|
||||
public Body m_Rigidbody;
|
||||
public ObjectPool<Body> m_Pool;
|
||||
public float m_SpawnTime;
|
||||
public int m_DefaultPoolCapacity;
|
||||
public int m_MaxPoolCapacity;
|
||||
private Vector3 m_PelvisPosition;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (m_Rigidbody == null)
|
||||
Debug.Log($"Rigidbody is null");
|
||||
|
||||
m_Pool = new ObjectPool<Body>(CreateBody, OnTakeBodyFromPool, OnReturnBodyToPool, OnDestroyBody, true, m_DefaultPoolCapacity, m_MaxPoolCapacity);
|
||||
|
||||
StartCoroutine(Spawn());
|
||||
}
|
||||
|
||||
private Body CreateBody()
|
||||
{
|
||||
Vector3 spawnPos = this.transform.position;
|
||||
Body body = Instantiate(m_Rigidbody, spawnPos, new Quaternion(Random.Range(0, 180), Random.Range(0, 180), Random.Range(0, 180), Random.Range(0, 180)));
|
||||
body.m_Pelvis.gameObject.GetComponent<Rigidbody>().AddForce(new Vector3(1, 1, 1));
|
||||
m_PelvisPosition = body.m_Pelvis.position;
|
||||
body.SetPool(m_Pool);
|
||||
return body;
|
||||
}
|
||||
|
||||
private void OnTakeBodyFromPool(Body body)
|
||||
{
|
||||
body.m_Pelvis.transform.position = m_PelvisPosition;
|
||||
body.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
private void OnReturnBodyToPool(Body body)
|
||||
{
|
||||
body.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void OnDestroyBody(Body body)
|
||||
{
|
||||
Destroy(body.gameObject);
|
||||
}
|
||||
|
||||
IEnumerator Spawn()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(m_SpawnTime);
|
||||
m_Pool.Get();
|
||||
StartCoroutine(Spawn());
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/8_Scripts/2_General/SpawnRB.cs.meta
generated
Normal file
11
Assets/8_Scripts/2_General/SpawnRB.cs.meta
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eb69c2a4007c792419505438a2761ba7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user