Animations get screwed up when I add OnAnimatorMove function
I'm using part of a code from one of the unity tutorials to figure out how to get my little orb to move with animations. The animations play fine before I add movement ie before I add OnAnimatorMove to set the movement for the rigidbody so I can move it around the scene with wsad. It'll play the idle in place and play the walk animation when I press wsad it just won't move, this is before adding OnAnimatorMove. Great right so I just have to add movement and it'll be fine. NOPE. Once I add the OnAnimatorMove function and call the rigidbody to move position, it will stop playing the idle animation at all and even though I can now move the orb around with wsad, its like, cycling through the walk animation in a weird fashion so that if I stop the orb it'll stop in the middle of the walk animation like a freeze frame. I'm unsure how to fix this and haven't been able to find a good answer online :T I'm very much a beginner at this. Help????
my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement2 : MonoBehaviour
{
public float turnSpeed;
Vector3 m_Movement;
Animator m_Animator;
Rigidbody m_Rigidbody;
//---------------------------------
//-----------------------------
void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();
m_Animator = GetComponent<Animator>();
}
//----------------------------------
//----------------------------------
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
m_Movement.Set(horizontal, 0f, vertical);
m_Movement.Normalize();
bool hasHorizontalInput = !Mathf.Approximately(horizontal, 0f);
bool hasVerticalInput = !Mathf.Approximately(vertical, 0f);
bool isWalking = hasHorizontalInput || hasVerticalInput;
m_Animator.SetBool("IsWalking", isWalking);
Vector3 desiredForward = Vector3.RotateTowards(transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f);
}
//-------------------------
//-------------------
void OnAnimatorMove()
{
m_Rigidbody.MovePosition(m_Rigidbody.position + m_Movement);
}
}
Your answer
Follow this Question
Related Questions
Switching lanes 1 Answer
How to make orb walking? 0 Answers
RTS character controller 1 Answer
My animation is working but the character won't move. 2 Answers