8way sprite with camera detection.

I’m working on a 3rd person prototype using sprites with billboarding and using Cinemachine did the camera controller and I’m trying to animate the sprites like how is done on Doom or Xenogears. I can make the character walk relative to the camera angle without issues, but animating the character based on the walk direction and camera angle isn’t working well. I’m using the Animator/Blend Tree approach with a 2D blend to make the animations so, for example, walk animation consists on a Blend Tree with 8 Animation Clips on it, like the below example:

So, using the X and Z of the direction vector I can set the animation to the character:

void GetPlayerToCamDir()
{
      anim.SetFloat("X", lookDir.x);
      anim.SetFloat("Y", lookDir.z);
}

8way movement without camera rotation works very well, but when taking the rotation from the camera and the movement direction, it’s just goes bonkers. This is part of the code of what I am using for movement (inside Update):

h = Input.GetAxisRaw("Horizontal");
v = Input.GetAxisRaw("Vertical");
direction = new Vector2(h, v).normalized;
if (direction.magnitude >= 0.1f) lastDirection = direction;
        
lookAngle = (Mathf.Atan2(lastDirection.x, lastDirection.y) * Mathf.Rad2Deg) - cam.transform.eulerAngles.y;
dirAngle = (Mathf.Atan2(lastDirection.x, lastDirection.y) * Mathf.Rad2Deg);
if (direction.magnitude >= 0.1f)
{
     spd = currentSpd;
     targetAngle = Mathf.Atan2(lastDirection.x, lastDirection.y) * Mathf.Rad2Deg + cam.transform.eulerAngles.y;
     movDir = (Quaternion.Euler(0, targetAngle, 0) * Vector3.forward).normalized;
     anim.SetBool("Walk", true);
}
else
{
     spd = 0;
     anim.SetBool("Walk", false);
}
spriteAngle = (targetAngle - dirAngle) - lookAngle;
lookDir = (Quaternion.Euler(0, spriteAngle, 0) * Vector3.forward).normalized;

My issues come from converting the angle of the camera and the angle of where is looking the character into a Vector3 that I can use on the Animator.

What can be done here?

Hi @PabloC3

Didn’t even check your code, but some notes…

I’ve tried this once. Although I built custom sprite animation thing to drive animations, it doesn’t matter what you use to animate the sprites. First you’ll have to have the billboard, that always faces the camera. Then you’ll have to find difference in angles between camera forward and character forward.

If character faces camera, then you get for example -180. When character faces right relative to camera forward, you get 90, if left -90, and if camera and character both face the same direction, you get 0. So based on this, you now can fetch correct facing direction based animations.

Example. Lets say you only use 4 directions, like in 1993 Wolfenstein (same, but less directions). If you have a facing value between -45 and 45, you know character is facing away from camera, so you get animations from rear set. If you get values between 45 and 135, your character is facing right relative to camera.