If you are using First Person movement and with a Turning type of Relative To Camera, you'll find that the Player's "Move speed float" parameter does not account for direction. Moving forwards, sideways and backwards always gives a positive value.
For games that show the player's body in this mode, it's likely that you want to be able to accurately animate them according to the direction they are moving in. This script controls Animator speed parameters in the forward and right directions, allowing for 2D Blend Tree creation.
To use it:
- Add two new float parameters, ForwardSpeed and RightSpeed, to your player's Animator Controller
- Create a new C# script named FirstPersonSpeedParameters and paste in the code below
- Attach the new First Person Speed Parameters component to your Player's root object
- Use the new float parameters to control animation as desired
FirstPersonSpeedParameters.cs:
using UnityEngine;
using System.Collections;
using AC;
public class FirstPersonSpeedParameters : MonoBehaviour
{
public string forwardSpeedParameter = "ForwardSpeed";
public string rightSpeedParameter = "RightSpeed";
private Animator _animator;
private AC.Char character;
void Start ()
{
character = GetComponent <AC.Char>();
_animator = character.GetAnimator ();
}
void Update ()
{
if (!string.IsNullOrEmpty (forwardSpeedParameter))
{
float forwardDot = Vector3.Dot (character.TransformForward, character.GetMoveDirection ());
_animator.SetFloat (forwardSpeedParameter, character.GetMoveSpeed () * forwardDot);
}
if (!string.IsNullOrEmpty (rightSpeedParameter))
{
float rightDot = Vector3.Dot (character.TransformRight, character.GetMoveDirection ());
_animator.SetFloat (rightSpeedParameter, character.GetMoveSpeed () * rightDot);
}
}
}