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.

124 lines
3.2 KiB

using System.Collections;
using System.Collections.Generic;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.Pool;
public class Body : MonoBehaviour
{
private ObjectPool<Body> m_Pool;
public Transform m_Pelvis;
public int m_DespawnTimer;
public bool m_JDActive;
public bool m_RandomActuations;
private float m_Noise;
private float m_NoiseX;
private float m_NoiseY;
private float m_NoiseZ;
[Header("Body Parts")]
public Transform m_Head;
public Transform m_Spine;
public Transform m_Left_Thigh;
public Transform m_Left_Leg;
public Transform m_Left_Foot;
public Transform m_Right_Thigh;
public Transform m_Right_Leg;
public Transform m_Right_Foot;
public Transform m_Left_Arm;
public Transform m_Left_Forearm;
public Transform m_Left_Hand;
public Transform m_Right_Arm;
public Transform m_Right_Forearm;
public Transform m_Right_Hand;
[Range(-1, 1)]
public float t;
public float m_NoiseSpeed;
private JointDriveController m_JDController;
private void OnEnable()
{
if(m_JDController == null && m_JDActive) {
m_JDController = this.GetComponent<JointDriveController>();
m_JDController.SetupBodyPart(m_Head);
m_JDController.SetupBodyPart(m_Spine);
m_JDController.SetupBodyPart(m_Left_Thigh);
m_JDController.SetupBodyPart(m_Left_Leg);
m_JDController.SetupBodyPart(m_Left_Foot);
m_JDController.SetupBodyPart(m_Right_Thigh);
m_JDController.SetupBodyPart(m_Right_Leg);
m_JDController.SetupBodyPart(m_Right_Foot);
m_JDController.SetupBodyPart(m_Left_Arm);
m_JDController.SetupBodyPart(m_Left_Forearm);
m_JDController.SetupBodyPart(m_Left_Hand);
m_JDController.SetupBodyPart(m_Right_Arm);
m_JDController.SetupBodyPart(m_Right_Forearm);
m_JDController.SetupBodyPart(m_Right_Hand);
}
m_NoiseX = Random.Range(-200, 200);
m_NoiseY = Random.Range(-200, 200);
m_NoiseZ = Random.Range(-200, 200);
if(!m_RandomActuations)
StartCoroutine(DespawnTimer());
//StartCoroutine(DespawnTimer());
}
public void SetPool(ObjectPool<Body> pool)
{
m_Pool = pool;
}
IEnumerator DespawnTimer()
{
yield return new WaitForSecondsRealtime(m_DespawnTimer);
m_Pool.Release(this);
}
private void FixedUpdate(){
if(m_RandomActuations)
RandomActutations();
}
public void RandomActutations(){
var bpDict = m_JDController.bodyPartsDict;
m_Noise += Time.fixedDeltaTime * 2;
var counter = 0;
foreach (var bodyPart in bpDict.Values){
bodyPart.SetJointTargetRotation(
Mathf.Clamp( (Mathf.PerlinNoise((Time.time + counter + m_NoiseX + 1) * m_NoiseSpeed, (Time.time + counter + m_NoiseX + 4) * m_NoiseSpeed) + -0.5f) * 3f, -1, 1),
Mathf.Clamp( (Mathf.PerlinNoise((Time.time + counter + m_NoiseY + 2) * m_NoiseSpeed, (Time.time + counter + m_NoiseY + 5) * m_NoiseSpeed) + -0.5f) * 3f, -1, 1),
Mathf.Clamp( (Mathf.PerlinNoise((Time.time + counter + m_NoiseZ + 3) * m_NoiseSpeed, (Time.time + counter + m_NoiseZ + 6) * m_NoiseSpeed) + -0.5f) * 3f, -1, 1)
);
bodyPart.SetJointStrength(1);
counter++;
}
// foreach (var bodyPart in bpDict.Values){
// bodyPart.SetJointTargetRotation(
// t, t, t
// );
// bodyPart.SetJointStrength(1);
// counter++;
// }
}
}