- Home /
Passing speed and direction to an Animator Controller
Hi everyone!
I'm new to Unity (especially to its coding side) so I've encountered a problem and I haven't been able to find the solution for days. I have a few Humanoid characters in my scene. Position of each of them is controlled by Simple Waypoint System and NavMeshAgent script. Speed and direction for animation blendtrees of every Animator Controller are passed by a Simple Waypoint System script called "MoveAnimator". Most of the time it works fine, but it overrides Root Motion in Animator. So whenever my character, for example, needs to sit down, he won't turn around and sit, he will just move his legs in place and will sit down on the wrong side of the chair. So I am wondering: is there any way to modify this script so it doesn't override the root motion? And if there's not: is there any other way of passing speed and direction of an object to its Animator Controller?
P.S. here's the MoveAnimator Script and the list of all the object's components:
/* This file is part of the "Simple Waypoint System" project by Rebound Games.
* You are only allowed to use these resources if you've bought them from the Unity Asset Store.
* You shall not license, sublicense, sell, resell, transfer, assign, distribute or
* otherwise make available to any third party the Service or the Content. */
using UnityEngine;
using DG.Tweening;
using UnityEngine.AI;
namespace SWS
{
public class MoveAnimator : MonoBehaviour
{
private splineMove sMove;
private NavMeshAgent nAgent;
private Animator animator;
private float lastRotY;
void Start()
{
animator = GetComponentInChildren<Animator>();
sMove = GetComponent<splineMove>();
if (!sMove)
nAgent = GetComponent<NavMeshAgent>();
}
void OnAnimatorMove()
{
//init variables
float speed = 0f;
float angle = 0f;
if (sMove)
{
speed = sMove.tween == null || !sMove.tween.IsPlaying() ? 0f : sMove.speed;
angle = (transform.eulerAngles.y - lastRotY) * 10;
lastRotY = transform.eulerAngles.y;
}
else
{
speed = nAgent.velocity.magnitude;
Vector3 velocity = Quaternion.Inverse(transform.rotation) * nAgent.desiredVelocity;
angle = Mathf.Atan2(velocity.x, velocity.z) * 2.0f / 3.14159f;
}
animator.SetFloat("Speed", speed);
animator.SetFloat("Direction", angle, 0.15f, Time.deltaTime);
}
}
}
Your answer
Follow this Question
Related Questions
Need help playing animation in reverse 1 Answer
Animator for Props 0 Answers
[Solvedf]Animator is not playing an AnimatorController 2 Answers