So I think this is a basic question, but I can’t seem to figure it out.
I have a series of ragdolls, all set up in a recycling object pool.
The problem is, when they are enabled and then physics are applied to them, the ragdoll components end up in a position other than their starting point.
I have tried adding an animation to the ragdoll to reset the position every time they are used, but it doesn’t seem to work.
Does anyone have a good solution for this? I really don’t want to use instantiate/destroy.
Hi everyone, the answer above did help point me in the right direction, but it’s not 100% correct, so I thought I would add my working script for any future people with the same issue.
using UnityEngine;
using System.Collections;
public class Ragdolls : MonoBehaviour {
public Animator zomAnim;
public Transform pelvis;
public Transform Lthigh;
public Transform Lcalf;
public Transform Rthigh;
public Transform Rcalf;
public Transform spine1;
public Transform head;
public Transform Lupperarm;
public Transform Lforearm;
public Transform Rupperarm;
public Transform Rforearm;
public Vector3 pelvisPosition;
public Vector3 LthighPosition;
public Vector3 LcalfPosition;
public Vector3 RthighPosition;
public Vector3 RcalfPosition;
public Vector3 spine1Position;
public Vector3 headPosition;
public Vector3 LupperarmPosition;
public Vector3 LforearmPosition;
public Vector3 RupperarmPosition;
public Vector3 RforearmPosition;
void Awake()
{
}
// Use this for initialization
void Start ()
{
GetChildPositions ();
StartCoroutine (ResetRagdoll());
}
void OnEnable()
{
GetChildPositions ();
StartCoroutine (ResetRagdoll());
}
// Update is called once per frame
void Update ()
{
}
public IEnumerator ResetRagdoll()
{
yield return new WaitForSeconds(5);
Debug.Log ("herpaderp");
this.gameObject.SetActive (false);
ReturnChildPositions();
PoolMaster.Despawn(gameObject);
}
public void GetChildPositions()
{
pelvisPosition = pelvis.position;
LthighPosition = Lthigh.position;
LcalfPosition = Lcalf.position;
RthighPosition = Rthigh.position;
RcalfPosition = Rcalf.position;
spine1Position = spine1.position;
headPosition = head.position;
LupperarmPosition = Lupperarm.position;
LforearmPosition = Lforearm.position;
RupperarmPosition = Rupperarm.position;
RforearmPosition = Rforearm.position;
}
public void ReturnChildPositions()
{
pelvis.position = pelvisPosition;
Lthigh.position = LthighPosition;
Lcalf.position = LcalfPosition;
Rthigh.position = RthighPosition;
Rcalf.position = RcalfPosition;
spine1.position = spine1Position;
head.position = headPosition;
Lupperarm.position = LupperarmPosition;
Lforearm.position = LforearmPosition;
Rupperarm.position = RupperarmPosition;
Rforearm.position = RforearmPosition;
}
public void FindChildren()
{
pelvis = this.transform.FindChild ("Bip01 Pelvis");
Lthigh = this.transform.FindChild ("Bip01 L Thigh");
Lcalf = this.transform.FindChild ("Bip01 L Calf");
Rthigh = this.transform.FindChild ("Bip01 R Thigh");
Rcalf = this.transform.FindChild("Bip01 R Calf");
spine1 = this.transform.FindChild ("Bip01 Spine1");
head = this.transform.FindChild ("Bip01 Head");
Lupperarm = this.transform.FindChild ("Bip01 L UpperArm");
Lforearm = this.transform.FindChild ("Bip01 L Forearm");
Rupperarm = this.transform.FindChild ("Bip01 R UpperArm");
Rforearm = this.transform.FindChild ("Bip01 R Forearm");
}
}