- Home /
The question is answered, right answer was accepted
Doom-like Sprite Models
Im currently with a project in development, an old-school doom-like fps, atm I'm using billboard shaders to render the enemies and objects, planes for the floor, ceiling and walls and cubes for objects (doors, buttons). The Doom's billboards have 7 images, each for an angle, what I need is: Check the angle relative to the player's current position.
You need to explain a bit more. Are you looking for the angle between the sprite and the player? What are you using to display the sprites (Plane, Quad, Custom Plane...)? How are you going to use this angle?
I'm using a textured Plane with a billboard shaded material. I need to know the angle between the player and the plane current rotation, to change it's sprite to the right one.
EDIT: http://www.youtube.com/watch?v=yr-lQZzevwA, the "3d models" are actually sprites, theres one sprite for each angle, and there are 7 angles, each adding 45°.
If you are using Plane such as created by Game Object>Create Other>Plane, then the visible surface is Up. You can get the angle using Vector3.Angle(player.transform.forward, plane.transform.forward.up). A cube has six sides/angles. What are the seven angles used by Doom?
You can see what I need there: http://www.doomlegends.com/emporium/tutorials/sprite_basics.html
Have a look here: http://wiki.unity3d.com/index.php?title=CameraFacingBillboard
I already got the billboard working, I just need to change the sprite according to the his angle relative to the player's view.
What do you mean, change the texture?? Please be more specific on what you mean by "Change the sprite".
Answer by robertbu · Sep 29, 2013 at 06:08 AM
Good reference. I can think of a few ways. Mathf.Atan2() will likely be the most efficient. First, you get a vector from the object to the player. Assuming each object is controlling its own texture it would be something like:
var dir = player.position - transform.position;
var angle = Mathf.Atan2(dir.z, dir.x);
if (angle < 0.0)
angle = angle + 360.0;
var index = Mathf.RoundToInt(angle / 45.0);
Note this is the angle on the XZ plane and disregards the 'y' component. I believe this is what you want. Also the index will go from 0 to 7 and, even if incremented by 1, the numbers will not line up with the number in the diagram. I think that 0 will line up with 7 and the numbers will increase counter clockwise, but you'll have to play and see.
You should have added $$anonymous$$athf.Rad2Deg;
Vector3 direction = Camera.main.transform.position - characterAIAgent.transform.position;
float angle = $$anonymous$$athf.Atan2(direction.z, direction.x) * $$anonymous$$athf.Rad2Deg;
if(angle<0.0f)
{
angle+=360.0f;
}
int index = $$anonymous$$athf.RoundToInt(angle / 45.0f);
Doing this gave me those two missing indexes, I tried using your method and I had to modify it in order to get more than 0 and 8 indexes.
By the way Doom actually uses 8 angles, OP.
Also you would need to take the object's rotation into account, not just the position.