This first example script connects user keyboard input with changes in Animator animationState. There is no logic to move the gameObject, or change values of the Transform Position vector values: x, y, z.
Simple Script to connect key-input to Animator Parameter: "HeroState".
using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassPlayerController : MonoBehaviour{//create custom data-typepublicenumHeroState { idle = 0, walk = 1, jump = 2} //custom data-type publicHeroState currentHeroState; //variable to keep track of current animation stateprivate bool facingRight =true; // initialize when declare (first way), false by defaultprivateAnimator animator; //obj-reference variable to access the animator component on this gameObject//inspector is the second way to initialize a value// Start is called before the first frame updateprivatevoidStart() { facingRight =true; //final way to initialize, overrides all prior settings animator = GetComponent<Animator>(); //<T> //call a method - make connection to component on gameObject in Unity scene
animator.SetInteger( "HeroState", (int) HeroState.idle ); }//Fixed update used for physics, to give smooth motion, called at consistent time incrementsvoidFixedUpdate() {float inputX =Input.GetAxis("Horizontal"); // key input: -1, 0, 1 bool isWalking =Mathf.Abs(inputX) >0;//check for spacebar or w, up keys to indicate vertical jump bool jumpPressed =Input.GetButtonDown("Jump") ||Input.GetAxis("Vertical") >0;if (isWalking) {animator.SetInteger("HeroState", (int)HeroState.walk); }else {animator.SetInteger("HeroState", (int)HeroState.idle); }if (jumpPressed) {animator.SetInteger("HeroState", (int)HeroState.jump); } }} //end class PlayerController