Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I call on Animations for idle, walk, and run

Discussion in 'Animation' started by EagleFlyt, Oct 31, 2016.

  1. EagleFlyt

    EagleFlyt

    Joined:
    Oct 31, 2016
    Posts:
    8
    I have been loosing my mind for a while now trying to learn all this. I finally switched all of my Animation from Legacy to Humanoid so that I could use the Animator Controller and yet I still am running into the same problem (And then some) I originally was trying to run the Animations through the Legacy and could not get the speed of the animations right in the scripting, nor able to call on Run animation whil I was walking. and so switched everything to Humanoid. Now I can not get it to change the Int so that it calls up the different animations AND it wobbles my Camera left and right with the Idle process... PLEASE I am totally lost on what to do. I am running 5.4


    Code (CSharp):
    1. public class UnitPlayer : Unit
    2. {
    3.     private Animator animator;
    4.  
    5.     // Use this for initialization
    6.     public override void Start()
    7.     {
    8.         animator = GetComponent<Animator>();
    9.         base.Start();
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     public override void Update ()
    14.     {
    15.         // rotation
    16.  
    17.         transform.Rotate (0f, Input.GetAxis ("Mouse X") * turnSpeed * Time.deltaTime, 0f );
    18.  
    19.         // movement
    20.  
    21.         move = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
    22.  
    23.         move.Normalize();
    24.  
    25.         move = transform.TransformDirection(move);
    26.         animator.SetInteger("Speed", 1);
    27.  
    28.         if (Input.GetKey (KeyCode.Space) && control.isGrounded)
    29.         {
    30.             jump = true;
    31.         }
    32.  
    33.         running = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
    34.         animator.SetInteger("Speed", 2);
    35.         // animation
    36.  
    37.  
    38.         base.Update();
    39.     }
    40. }
    41.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof (CharacterController))]
    5.  
    6. public class Unit : MonoBehaviour
    7. {
    8.  
    9.     protected CharacterController control;
    10.  
    11.     protected Vector3 move = Vector3.zero;
    12.  
    13.     public float walkSpeed = 4f;
    14.     public float runSpeed = 8f;
    15.     public float turnSpeed = 15f;
    16.     public float jumpSpeed = 5f;
    17.  
    18.     protected bool jump;
    19.     protected bool running;
    20.  
    21.     protected Vector3 gravity = Vector3.zero;
    22.  
    23.     // Use this for initialization
    24.     public virtual void Start()
    25.     {
    26.  
    27.         control = GetComponent < CharacterController >();
    28.  
    29.         if (!control)
    30.         {
    31.             Debug.LogError("Unit.Start[) " + name + " has no CharacterController!");
    32.             enabled = false;
    33.         }
    34.     }
    35.  
    36.     // Update is called once per frame
    37.     public virtual void Update()
    38.     {
    39.         // control.SimpleMove(move * moveSpeed);
    40.  
    41.         if (running)
    42.             move *= runSpeed;
    43.         else
    44.             move *= walkSpeed;
    45.  
    46.         if (!control.isGrounded)
    47.         {
    48.             gravity += Physics.gravity * Time.deltaTime;
    49.         }
    50.         else
    51.         {
    52.             gravity = Vector3.zero;
    53.  
    54.             if (jump)
    55.             {
    56.                 gravity.y = jumpSpeed;
    57.                 jump = false;
    58.             }
    59.         }
    60.  
    61.         move += gravity;
    62.  
    63.         control.Move(move * Time.deltaTime);
    64.     }
    65. }
    66.  
    and this is my failed attempt at making the Animations work with Legacy and everything in Javascript

    Code (CSharp):
    1. #pragma strict
    2.  
    3. function Start () {
    4.  
    5.  
    6. }
    7.  
    8. function Update () {
    9.  
    10.     if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
    11.     {
    12.         //animator.SetInteger("Speed", 1);
    13.  
    14.     }
    15.     else
    16.         // (Mathf.Abs(Input.GetAxis("Vertical")) < 0.1)
    17.     {
    18.        // animator.SetInteger("Speed", 0);
    19.     }
    20.  
    21.     if (Mathf.Abs(Input.GetAxis("Vertical")) > 8)
    22.     {
    23.        // animator.SetInteger("Speed", 2);
    24.     }
    25. }
    26.    // if (Input.GetKey ("w") || Input.GetKey ("s"))
    27.     //{
    28.       //  GetComponent.<Animation>().Play("Walk");
    29.  
    30.     //}
    31.     //{
    32.       //  if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift))
    33.         //{
    34.           //  GetComponent.<Animation>()["Run"].speed = 10.0f;
    35.            // GetComponent.<Animation>().CrossFade("Run");
    36.        // }
    37.        // else
    38.         //{
    39.          //   GetComponent.<Animation>().CrossFade("Idle");
    40.         //}
    41.   //  }

    Someone PLEASE help!
     
  2. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Why not just set the speed as a Float in Mecanim? That way you could do all your transitioning based on the actual speed value instead of trying to partition it out into pieces.
     
  3. EagleFlyt

    EagleFlyt

    Joined:
    Oct 31, 2016
    Posts:
    8
    Because from the tutorials I read about using a float, the animations would just quickly transition from Idle to run barely playing the Walk. I want the Player to be able to walk AND run by hitting the Shift key and only when the Shift key is pressed.
     
  4. EagleFlyt

    EagleFlyt

    Joined:
    Oct 31, 2016
    Posts:
    8
    *** Walk by w and run by w and shift **** <--- clarification
     
  5. EagleFlyt

    EagleFlyt

    Joined:
    Oct 31, 2016
    Posts:
    8
    Ok, I started over on the Animation yet again... Here is the code I am using now...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerScript : MonoBehaviour
    5. {
    6.     Animator anim;
    7.     int jumpHash = Animator.StringToHash("Jump");
    8.     int runStateHash = Animator.StringToHash("Base Layer.Run");
    9.  
    10.  
    11.     void Start()
    12.     {
    13.         anim = GetComponent<Animator>();
    14.     }
    15.  
    16.  
    17.     void Update()
    18.     {
    19.         float move = Input.GetAxis("Vertical");
    20.         anim.SetFloat("Speed", move);
    21.  
    22.         AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
    23.         if (Input.GetKeyDown(KeyCode.Space) && stateInfo.nameHash == runStateHash)
    24.         {
    25.             anim.SetTrigger(jumpHash);
    26.         }
    27.     }
    28. }
    But as stated in previous Reply, It switches straight through the walk animation to Run animation without me pressing Shift key. How do I, (And do not mind watching or reading a tutorial on this) make it where my player only runs when I hit shift?

    Also I am running into a problem where (even though I set the Speed Param to 0.1) it takes about a half second to a second for the animation to switch from Idle to anything else.

    I am also still having the problem where the camera is swaying left and right with the Idle animation.
     
  6. GrischaG

    GrischaG

    Joined:
    Apr 26, 2013
    Posts:
    40
    Take a look at your Transitions in the AnimatorController. There are a few options that change the behavior.
    Check out what "Has exit time" does for example.
    You should post a picture of your graph for better help ;)
     
  7. EagleFlyt

    EagleFlyt

    Joined:
    Oct 31, 2016
    Posts:
    8
  8. EagleFlyt

    EagleFlyt

    Joined:
    Oct 31, 2016
    Posts:
    8
  9. GrischaG

    GrischaG

    Joined:
    Apr 26, 2013
    Posts:
    40
    Why do you use the getaxis vertical? Thats the mouse input.
    Try getkeydown w.
    And then you just need two bools in your animator for walk and run and dependent translations.
     
  10. EagleFlyt

    EagleFlyt

    Joined:
    Oct 31, 2016
    Posts:
    8
    I tried the getkeydown and it continuously played walk even when I have the shift key pressed with W for run using getkeydown as well. So I was told to switch to float. Please understand I am a noob when it comes to Unity and C# so please explain further.
     
  11. GrischaG

    GrischaG

    Joined:
    Apr 26, 2013
    Posts:
    40
    Did you adjust your Transitions? Entry -> Idle - (Walk = true) -> Walk - (Run = true) -> Run
     
  12. EagleFlyt

    EagleFlyt

    Joined:
    Oct 31, 2016
    Posts:
    8
    yes. thats ok, I am starting over with a new style script. I will see if that works.