Search Unity

Need help with animation

Discussion in 'Scripting' started by Battin, Apr 21, 2015.

  1. Battin

    Battin

    Joined:
    Apr 8, 2015
    Posts:
    11
    Hi, this is my first time posting a thread on the Unity Forums, so bare with me :)

    Since a few weeks I've been playing around with Unity and so far I love it. Have build some nice levels and have a great idea for a game.

    Been following tutorials from -Digital Tutors- on the introduction and creating your first game tutorials and lots more from Unity itself.

    Now I'm not a scripter and never used C# extensively before. So I'm quite happy about the fact that I got this far with little understanding of what it is (haha)

    The thing is: I got a basic movement on my player (which is walking)
    I can change the parameters and animations of the animation, aswell as the code that I made for the walking.

    So I have the animation "walking" (which works) but want to add a running animation when you press and hold the "Left Shift" key.

    How can I place that correctly in my script (which i learned from DT).

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CharacterMovement : MonoBehaviour
    6.     {
    7.         public float speed = 6f;            //The speed that the player will move.
    8.         public float turnSpeed = 60f;        //
    9.         public float turnSmooting = 15f;
    10.  
    11.         private Vector3 movement;
    12.         private Vector3 turning;
    13.         private Animator anim;
    14.         private Rigidbody playerRigidbody;
    15.  
    16.         void Awake ()
    17.         {
    18.             //Get references
    19.             anim = GetComponent<Animator>();
    20.             playerRigidbody = GetComponent<Rigidbody>();
    21.         }
    22.  
    23.         void FixedUpdate()
    24.         {
    25.             //Store input axes
    26.             float lh = Input.GetAxisRaw ("Horizontal");
    27.             float lv = Input.GetAxisRaw ("Vertical");
    28.  
    29.             Move (lh, lv);
    30.  
    31.             Animating (lh, lv);
    32.         }
    33.  
    34.         void Move(float lh, float lv)
    35.         {
    36.             //Move the player
    37.             movement.Set (lh, 0f, lv);
    38.  
    39.             movement = movement.normalized * speed * Time.deltaTime;
    40.  
    41.             playerRigidbody.MovePosition (transform.position + movement);
    42.  
    43.             if(lh != 0f || lv != 0f)
    44.                 {
    45.                     Rotating (lh, lv);
    46.                 }
    47.  
    48.         }
    49.  
    50.     void Rotating(float lh, float lv)
    51.     {
    52.         Vector3 targetDirection = new Vector3 (lh, 0f, lv);
    53.  
    54.         Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up);
    55.         Quaternion newRotation = Quaternion.Lerp (GetComponent<Rigidbody> ().rotation, targetRotation, turnSmooting * Time.deltaTime);
    56.         GetComponent<Rigidbody> ().MoveRotation (newRotation);
    57.     }
    58.  
    59.         void Animating(float lh, float lv)
    60.         {
    61.             bool running = lh != 0f || lv != 0f;
    62.             anim.SetBool ("IsRunning", running);
    63.         }
    64.  
    65.     }
    66.  
    Thank you in advance.

    PS: If there is another way to get a script to work in a faster or more understandable way :p, please.
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Did you check with Debug.Log if your running gets true ever? And for the Input, you should check the Input.GetKeyDown API on Unity, there is also an example code how to use it. Another thing to address is the Animator tutorial, where you can put your Running Animation in, and if "IsRunning" is true, it should go over to the running animation.