implementation of drecon in unity 2022 lts forked from: https://github.com/joanllobera/marathon-envs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.1 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
public class BodyManager : MonoBehaviour
{
public static BodyManager _instance { get; private set; }
public Body m_Rigidbody;
public ObjectPool<Body> m_Pool;
public float m_SpawnTime;
public int m_DefaultPoolCapacity;
public int m_MaxPoolCapacity;
public Transform m_Parent;
public Transform m_SpawnParent;
private Vector3 m_PelvisPosition;
private void Awake()
{
if (_instance != null)
Destroy(this);
else
_instance = this;
}
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 = new Vector3(this.transform.position.x + Random.Range(-0.2f, 0.2f), this.transform.position.y, this.transform.position.z + Random.Range(-0.2f, .2f));
Body body = Instantiate(m_Rigidbody, m_SpawnParent.position, new Quaternion(Random.Range(0, 180), Random.Range(0, 180), Random.Range(0, 180), Random.Range(0, 180)), m_Parent);
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_SpawnParent.position;
body.gameObject.SetActive(true);
}
private void OnReturnBodyToPool(Body body)
{
body.gameObject.SetActive(false);
}
private void OnDestroyBody(Body body)
{
Destroy(body.gameObject);
}
IEnumerator Spawn()
{
5 months ago
// 0.75, 1.25f <remove realtime for recording>
yield return new WaitForSecondsRealtime(Random.Range(1f, 2.5f));
m_Pool.Get();
StartCoroutine(Spawn());
}
}