- Home /
How do you make 4 sided Doom like directional sprites?
I have tried every single thing I tried animator floats that trigger the transitions according to the rotation it would happen at.
All of the maths and scripts line up with the -179 to 179 ratio of rotation. Also it is an enemy that is acting on 3D plane as a 2D object and that means it has a trigger to stop animations once you get near it which means that the attempt to put another childed object that has a collider disabling the not near me animation trigger I have doesn't work because I am shooting with raycasts which will identify itself as hitting the collider of the childed object used to disable the trigger on the enemy so that it can start attacking.
Also the enemy will have 3 hit boxes one on the arm, another on the stomach and one on the head.
In my game you can't jump or rotate the camera up for raycast shooting like in Doom. The problem with adding box colliders to be animation triggers is that the ontriggerenter command I have immediately turns off the not near me trigger so it can attack.
Is there any approach to this I can take or anyway to make directional sprites somebody knows I can use.
The enemy will always face forward once you have disabled the not near me trigger.
Answer by YoannHx · May 22, 2020 at 09:16 PM
Hi. Sorry your post is a bit confuse... I hope I am understanding correctly that your current problem is to display the correct sprite depending on the orientation of the enemy relatively to the camera.
If that's so I would suggest to simply delegate that to the shader!
I was needing some practice with Shader Graph so... I grabbed some sprites and made a shader and a simple scene.
The shader is made for some spritesheets with animation frames along X and angles along Y, you can tune the number of frames and angles (8 is so much more pleasant to the eye than 4 ;-) ), as well as animation speed simply by changing the shader exposed properties (see the use of MaterialPropertyBlock in the attached CharacterController script).
Here's :
the complete Unity project (2019.3.14/URP) (it's a temporary Weshare link, grab it while it's alive! I'll update this post when it's cleaned and uploaded on Github)
Feel free to use (sprite credits in the project files).
In case you prefer to go for the script way, you can do something similar to the original post of this forum thread:
int GetAngleIndex()
{
var dir = GetComponent<Camera>().transform.position - transform.parent.forward;
var enemyAngle = Mathf.Atan2(dir.z, dir.x) * Mathf.Rad2Deg;
if (enemyAngle < 0.0f)
enemyAngle += 360;
Debug.Log("Angle from the player is: " + enemyAngle);
if (enemyAngle >= 292.5f && enemyAngle < 337.5f)
return 8;
else if (enemyAngle >= 22.5f && enemyAngle < 67.5f)
return 2;
else if (enemyAngle >= 67.5f && enemyAngle < 112.5f)
return 3;
else if (enemyAngle >= 112.5f && enemyAngle < 157.5f)
return 4;
else if (enemyAngle >= 157.5f && enemyAngle < 202.5f)
return 5;
else if (enemyAngle >= 202.5f && enemyAngle < 247.5f)
return 6;
else if (enemyAngle >= 247.5f && enemyAngle < 292.5f)
return 7;
else if (enemyAngle >= 337.5f || enemyAngle < 22.5f)
return 1;
else return 0;
}
You can adjust it to use 4 sprites instead of 8 and use the output of the function to select the correct sprite in your sprite sheet.
Help! @YoannHx @YoannHx Are you telling me with the script that according that I can apply a script to the enemy to get the angle and then if it is that angle then it returns the number back to the animator variable and I set the animator variables and if statements of the animator accordingly for my angles? I get all of what you're trying to say can you just help me with applying all of the numbers the equation needs properly so it won't have error. In summary I get it I just need to know how to apply it properly if you could help me with that it would be great. Also the Getangleinderx is invalid Please help just a bit more @YoannHx.
Answer by frilanski · May 22, 2020 at 09:14 PM
If I follow some of the issue (I think you lost me towards the end) I would use the transform.LookAt() to have the 2d sprite face the player at all times like in doom. As for the raycast. You can pass the raycast a layermask. If you put the child object that has the distance collider on a different layer to the enemy that has the hitboxes on then the raycast will ignore the child collider and just hit the enemy.
Hope this helps.