Hello
In the Ragdoll lesson, Turn based course, I wanted to give a go at modifying the AddExplosionForce position.
Instead of setting it at the foot of the target, I wanted to capture the firing angle and offset the explosion position in the opposite direction of the firing angle, by a couple of units outside of the model and 1 unit up.
In the RagdollSpawner script, I created class members to record and store the firing angles
private Vector3 firingAngle = new Vector3(0, 0, 0);// PG
private List listShotsFired = new List();
They will be update by the OnShoot event - see below.
I then suscribed to OnShoot event in the Awake - ideal as EventArgs stores the shooter and target- and defined the function:
All the code is working fine, the debug shows I can record the number of shots fired and their angles, including the last shot killing the enemy… No problem so far…
private void ShootAction_OnShoot(object sender, ShootAction.OnShootEventArgs e)
{
Debug.Log("E.Shooting position = " + e.shootingUnit.transform.position);
Debug.Log("E.Target position = " + e.targetUnit.transform.position);
firingAngle = (e.targetUnit.transform.position - e.shootingUnit.transform.position).normalized;
Debug.Log("FiringAngle = "+ firingAngle);
listShotsFired.Add(firingAngle);
for(int i =0; i<listShotsFired.Count;i++)
{
Debug.Log("Shot " + i + " angle: " + listShotsFired[i]);
}
}
It all goes wrong when the existing HealthSystem_OnDead fires up in the same class.
When this event happens, it is time to use those variables we were recording…
Unfortunately, they are all null now !!!
private void HealthSystem_OnDead(object sender, EventArgs e)
{
int shotsFired = listShotsFired.Count - 1;// the list is now empty, all reset to 0!!!
firingAngle = listShotsFired[shotsFired];// listShotsFired now empty * CRASH *
Transform ragdollTransform = Instantiate(ragdollPrefab, transform.position, transform.rotation);
UnitRagdoll unitRagdoll =ragdollTransform.GetComponent<UnitRagdoll>();
unitRagdoll.Setup(originalRootBone, firingAngle);// runs fine but passes a null firingAngle
}
All the variables I had created seem to have lost their values, why is this happening?
There is no other function or event in this class, just the awake to subscribe and both events above.
Thanks for your help.