Question by
terry213 · Nov 06, 2016 at 09:17 AM ·
inputanimator controlleranimationclipgetaxiscrossfade
How to make character animate as he's rotating?
Hello,
I am currently trying to figure out how I would script a character animating as he is rotating in place. I have the basic move and jump scripted but I cant seem to get the turn working.
Here is the code:
using UnityEngine; using System.Collections;
public class CharacterMovement : MonoBehaviour {
private CharacterController controller;
public float movementSpeed = 10;
public float turningSpeed = 60;
private Vector3 moveDirection = Vector3.zero;
public float gravity = 20;
public float jumpSpeed = 8;
private Animator anim;
void Start ()
{
controller = GetComponent <CharacterController> ();
anim = gameObject.GetComponentInChildren<Animator> ();
}
void Update() {
float turn = Input.GetAxis ("Horizontal");
transform.Rotate (0, turn * turningSpeed * Time.deltaTime, 0);
if (controller.isGrounded) {
moveDirection = transform.forward * Input.GetAxis ("Vertical") * movementSpeed;
anim.SetFloat ("speed", controller.velocity.magnitude);
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
if (Input.GetButton ("Jump")) {
anim.CrossFade ("jump", 0.2f);
} else {
anim.CrossFade ("run", 0.2f);
}
}
}
controller.Move (moveDirection * Time.deltaTime);
moveDirection.y -= gravity * Time.deltaTime;
if (Input.GetKey ("Horizontal")) {
anim.CrossFade ("jog", 0.2f);
if (Input.GetButtonUp ("Horizontal"))
anim.CrossFade ("idle", 0.2f);
} else {
anim.CrossFade ("jog", 0.2f);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612100130im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera animation clip stuck on first frame 0 Answers
Animating in c# 1 Answer
Animation changes when marked as legacy 0 Answers
how to Restore corrupted Animator 0 Answers
Animation looks super janky while the game is running 0 Answers