- Home /
How to move character with animation
I want the character move forward with keyW.It does work when I click " Apply root motion " which under Animator, but the animation doesn't play. And if I didn't click it, character plays the animation with none movement.
Down below is my character code.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class player_control : MonoBehaviour { private float speed = 5f; private float rotspeed = 80; private float gravity = 8; private float rot = 0f;
Vector3 moveDir = Vector3.zero;
CharacterController controller;
Animator anim;
// Use this for initialization
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.W))
{
anim.SetBool("Walk",true);
moveDir = new Vector3(0, 0, 1);
moveDir *= speed;
moveDir = transform.TransformDirection(moveDir);
}
if(Input.GetKeyUp(KeyCode.W))
{
anim.SetBool("Walk", false);
moveDir = new Vector3(0, 0, 0);
}
rot += Input.GetAxis("Horizontal") * rotspeed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, rot, 0);
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
}
Answer by cs120319992 · Jan 18, 2019 at 01:41 PM
Animations is connected correctly in animator controller? Screen?
Answer by hai0909 · Jan 18, 2019 at 04:09 PM
Yab, I guess. Hope it doesn't have mistake in there. ![alt text][1] [1]: /storage/temp/131399-1547827464678.jpg
Answer by mar10 · Jan 20, 2019 at 12:27 AM
check your animation import settings Animation tab the root transform position (XZ) bake into pose should not be checked & don't move the controller just turn it, also your animation must have movement from point a to point b location not just motion.
Your answer
Follow this Question
Related Questions
can i use character from CHARACTER CREATOR 2D in other animation software 0 Answers
Creating character animations from within Unity. 0 Answers
How to play animation on keypress 2 Answers
How to start an animation from the end of another animation? 1 Answer
1st Person Controller with Full Character Animation? 0 Answers