- Home /
Question by
bagdad610 · Jul 09, 2017 at 08:34 PM ·
animationmovementscript.animator controller
Aligning animation with movement.
So the problem is when I move the character(he is not humanoid) his walk animation is not aligned with the direction of his movement, for example, if I press W then he moves forward but his walk animation turns 90 degrees left, I have no idea where the problem might be. Here's the code I have:
public class PlayerControler : MonoBehaviour {
public float MovSpeed = 3;
public float SmoothTime = 0.1f;
float TurnSmoothVelocity;
public float SpeedSmoothTime = 0.1f;
float SmoothVelocity;
float CurrentSpeed;
Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
Vector2 Input = new Vector2(UnityEngine.Input.GetAxisRaw("Horizontal"), UnityEngine.Input.GetAxisRaw("Vertical"));
Vector2 InputDirection = Input.normalized;
if (InputDirection != Vector2.zero)
{
float TargetRotation = Mathf.Atan2(InputDirection.x, InputDirection.y) * Mathf.Rad2Deg;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y , TargetRotation, ref TurnSmoothVelocity, SmoothTime);
}
float targetSpeed = MovSpeed * InputDirection.magnitude;
CurrentSpeed = Mathf.SmoothDamp(CurrentSpeed, targetSpeed, ref SmoothVelocity, SmoothTime);
transform.Translate(transform.forward * CurrentSpeed * Time.deltaTime, Space.World);
float animationSpeedPercent = .5f * InputDirection.magnitude;
animator.SetFloat("SpeedPercent", animationSpeedPercent, SpeedSmoothTime, Time.deltaTime);
}
}
Thanks in advance :D
Comment