# Rotate object to match another after fall!

In my game, when enemies get shot they are given some random force so that they land on the ground in all sorts of random rotations. However after a few seconds I want graves to appear over them. However i’m having trouble matching the rotation because the enemies rotations are all over the place, and the graves sometimes end up looking to match but are actual 180 degrees out, or sometimes just wrong altogether. Any pointers on how I can correct this? My current code is at the bottom.
Here are the fallen enemies:

And here are the graves after a few seconds. The far right one looks about right, but the other 2 are 180 degrees out. And some times are around 90 degrees out.

``````// wait for a few seconds before disappearing
yield return new WaitForSeconds(5);

Transform deathTransform = this.transform;

var tomb = Instantiate(TombPrefab);
tomb.transform.position = new Vector3(deathTransform.position.x, deathTransform.position.y - tombYoffset, deathTransform.position.z);

Vector3 eulerRotation = new Vector3(tomb.transform.eulerAngles.x, deathTransform.eulerAngles.y, tomb.transform.eulerAngles.z);
tomb.transform.rotation = Quaternion.Euler(eulerRotation);

Destroy(this.gameObject);
``````

Dude, this is GRUESOME! I love it.

So i think this will get you what you want. Here’s how I am thinking about it.

As long as he’s not standing upwards, an arrow shooting out of the top of a soldier’s head aligns with which way you want his gravestone to be placed, correct?

Therefore, just take the `transform.up` shortcut off the dead soldier, flatten the vector (set the .y to zero), and normalize it.

That vector could be subsequently used to spin the grave around. If the grave was made “normal” so the cross (head) end was going +Z local, then you could just set the grave’s `transform.forward` vector equal to the above flattened / normalized vector.

Just as an aside, here is why what you are trying (as reasonable as it seems) does not work all cases:

All about Euler angles and rotations, by StarManta:

https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

1 Like

Haha glad you like it… it means a lot to get nice feedback

Yeah that makes sense, rotation and direction still confuses me quite a lot I need to read into this again to get it right in my head.

I’ll give your suggestion ago tomorrow… I have a feeling I’ll be asking some follow up questions to get this right.

Thanks again.

Many thanks for your help @Kurt-Dekker and for the useful link, I got it working just as you suggested
Here is the code:

``````// Get centre position to use to spawn grave from
Transform deathTransform = this.transform.Find("CentrePoint").transform;
var deathDirection = transform.up.normalized;

var grave = Instantiate(TombPrefab);
grave.transform.position = new Vector3(deathTransform.position.x, deathTransform.position.y - graveYoffset, deathTransform.position.z);

grave.transform.forward = deathDirection;
``````
2 Likes