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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Animation Script Help

Discussion in 'Scripting' started by UltiGamer835, Oct 29, 2015.

  1. UltiGamer835

    UltiGamer835

    Joined:
    Jun 5, 2013
    Posts:
    8
    Hey, I've been searching around the forums to help maybe wrap my head around this, but I can't seem to get this to work. I'm trying to allow my character to "shoot" (play a recoil animation) while walking (walking animation). I can't cross fade the recoil animation because it won't play all the way through, and I tried Animation.Blend but it didn't seem to work. If anyone could help me out that would be great. Also, the recoil animation in held in a child object of the object holding the walking animation, if that helps any.

    Code (CSharp):
    1. //Walking animations
    2. if (walkingState == WalkingState.Running && !IsAiming && walkingState != WalkingState.Idle && VelocityMagnitude > 5.5f)
    3.         {
    4.             WalkAnimationHolder.animation["M1911Run"].speed = VelocityMagnitude / RunSpeed;
    5.             WalkAnimationHolder.animation.CrossFade("M1911Run", 0.5f);
    6.             CameraAnimationHolder.animation["Camera Run 1"].speed = VelocityMagnitude / RunSpeed;
    7.             CameraAnimationHolder.animation.CrossFade("Camera Run 1", 0.5f);
    8.         }
    9.         else if (walkingState == WalkingState.Walking && VelocityMagnitude < 5.5f || VelocityMagnitude > 3.5f)
    10.         {
    11.             WalkAnimationHolder.animation["M1911Walk"].speed = VelocityMagnitude / WalkSpeed;
    12.             WalkAnimationHolder.animation.CrossFade("M1911Walk",0.5f);
    13.             CameraAnimationHolder.animation["Camera Walk 1"].speed = VelocityMagnitude / WalkSpeed;
    14.             CameraAnimationHolder.animation.CrossFade("Camera Walk 1", 0.5f);
    15.  
    16.         }
    17.         else
    18.         {
    19.             WalkAnimationHolder.animation.CrossFade("M1911Idle",0.2f);
    20.             CameraAnimationHolder.animation.CrossFade("Camera Idle 1", 0.5f);
    21.         }
    22.  
    23. //Recoil animations
    24. if (walkingState != WalkingState.Running) {
    25.                                         if (shootime <= 0 && !RecoilHolder.animation.isPlaying) {
    26.                                                 CurrentRotation = Quaternion.Slerp (CurrentRotation, CurrentWeapon.Recoil, Time.deltaTime);
    27.                                                 CamRotation = Quaternion.Slerp (CamRotation, NextCamRotation, 2 * Time.deltaTime);
    28.                                                 CurrentPosition = Vector3.Lerp (CurrentPosition, CurrentWeapon.RecoilKick, Time.deltaTime);
    29.                                                 RecoilHolder.animation.Play("M1911Recoil");
    30.                                                 //CamRotation = CamRotation * new Quaternion(-1, 0, 0, 0);
    31.                                                 shootime = 1;
    32.                                         }
    33.                                         shootime -= Time.deltaTime * CurrentWeapon.firerate;
    34.                                         //CurrentRotation = Quaternion.Slerp(CurrentRotation, NextRotation, 0.35f);
    35.                                         //CurrentPosition = Vector3.Lerp(CurrentPosition, NextPosition, 0.15f);
    36.                                 }
     
  2. UltiGamer835

    UltiGamer835

    Joined:
    Jun 5, 2013
    Posts:
    8