Forum rules - please read before posting.

Angle parameter for 3D Mecanim characters

Hi

I'm creating a 2d sidescrolling game but using 3d characters. When the character walks left or right, the direction automatically shows the side profile of the character.
I'm trying to make it so that when walking in either direction, the character slightly faces the camera, at maybe a 45 degree angle as seen in this screenshot of my previous game - https://assets.rockpapershotgun.com/images/2019/02/01_rainswept.jpg

The best way to do this seems to be to have separate animations for walking left and for walking right, and calling them using the Angle parameter. In Sprites Unity Complex, there's an angle parameter that shows either 90 or 270 degrees depending on whether the character is moving left or right, but there's no such parameter for Mecanim animation engine in AC. Is there any way to use this angle information of the character to do what I need? Is there any other way to achieve that angle?

I also tried just adding a bit of angle to character in the beginning of the common walk animation, but of course that only works for one direction. For the opposite direction, the character faces their back to the camera at the same angle.

Thanks!

Comments

  • edited October 2019

    The parameter used by Sprites Unity Complex is relative to the camera, which is necessary in a 2D game. When using Mecanim, this isn't typically the case - as the camera is more often free-moving.

    However, calculating this angle value is pretty simple and can be done in a custom script:

    using UnityEngine;
    
    public class MecanimAngleParameter : MonoBehaviour
    {
    
        public Animator _animator;
    
        void Update ()
        {
            float forwardAmount = Vector3.Dot (Vector3.forward, transform.forward);
            float rightAmount = Vector3.Dot (Vector3.right, transform.forward);
    
            float facingAngle = Mathf.Atan (rightAmount / forwardAmount) * Mathf.Rad2Deg;
            if (forwardAmount > 0f) facingAngle += 180f;
    
            _animator.SetFloat ("Angle", facingAngle);
        }
    
    }
    
  • Thank you! I shall give this a shot soon and let you know how it goes.

  • Updated with a quick correction.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.