/// <summary> /// 播放一个动画,默认过渡时间为0.3s,如果在此期间再次播放,则会继续播放 /// </summary> /// <param name="stateTypes"></param> /// <param name="fadeDuration">动画过渡时间</param> /// <returns></returns> public AnimancerState PlayAnim(StateTypes stateTypes, float fadeDuration = 0.3f, float speed = 1.0f) { AnimancerState animancerState = AnimancerComponent.CrossFade(this.AnimationClips[RuntimeAnimationClips[stateTypes]], fadeDuration); animancerState.Speed = speed; return(animancerState); }
/************************************************************************************************************************/ public void CrossFade() { _Animancer.CrossFade(_Action); // The optional 'fadeDuration' parameter specifies how long it will take (in seconds). // _Animancer.CrossFade(_Action, 0.5f);// This will take 0.5 seconds. // If not specified, the default duration is 0.3 seconds. }
/************************************************************************************************************************/ private void Update() { if (Input.GetMouseButtonDown(0)) { // Play the action animation and register a callback for when it finishes. var state = _Animancer.CrossFade(_Action); state.OnEnd = OnActionEnd; } }
/************************************************************************************************************************/ private void Update() { // Get the movement and clamp it so we don't move faster diagonally. var input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); input = Vector2.ClampMagnitude(input, 1); var isMoving = input != Vector2.zero; var isSprinting = Input.GetButton("Fire3");// Left Shift by default. // Gradually move the movement parameter towards the target value. var movement = Vector2.MoveTowards(_Locomotion.State.Parameter, input, _AnimationAcceleration * Time.deltaTime); _Locomotion.State.Parameter = movement; _Locomotion.State.Speed = (isMoving && isSprinting) ? _SprintMultiplier : 1; if (isMoving || isSprinting) { if (!_IsAwake) { _IsAwake = true; // Make sure the graph is unpaused (because we pause it when going back to sleep). _Animancer.Playable.UnpauseGraph(); // Play the wake up animation forwards. var state = _Animancer.CrossFade(_WakeUp); state.Speed = 1; // When finished waking up, start moving. state.OnEnd = _FadeToMovement; } } else { if (_IsAwake) { _IsAwake = false; // Play the wake up animation backwards. var state = _Animancer.CrossFade(_WakeUp); state.Speed = -1; // If it was not already playing, start at the end of the animation. // Otherwise just play backwards from the current time. if (state.Weight == 0 || state.NormalizedTime > 1) { state.NormalizedTime = 1; } // When finished going to sleep, pause the graph. state.OnEnd = _PauseGraph; } } }
/************************************************************************************************************************/ private void Awake() { // Start paused at the beginning of the animation. _Animancer.Play(_WakeUp); _Animancer.Playable.Evaluate(); _Animancer.Playable.PauseGraph(); // Cache the delegates we will use with the OnEnd event so we don't allocate garbage every time. _PauseGraph = _Animancer.Playable.PauseGraph; _FadeToMovement = () => _Animancer.CrossFade(_Move); }
private void GroundAnimation() { if (vInput > 0 || vInput < 0) { if (!animancer.IsPlaying(run)) { animancer.CrossFade(run); } } else if (!animancer.IsPlaying(idle)) { animancer.CrossFade(idle); } }
public void ToggleRunning() { _IsRunning = !_IsRunning; if (_IsRunning) { _BasicAnimancer.CrossFade(_Run); _LayeredAnimancer.CrossFade(_Run); } else { _BasicAnimancer.CrossFade(_Idle); _LayeredAnimancer.CrossFade(_Idle); } }
private void GroundAnimation() { if (vInput > 0) { if (!animancer.IsPlaying(nonCombatAnimationClips[1])) { animancer.CrossFade(nonCombatAnimationClips[1]); } } else if (vInput < 0) { if (!animancer.IsPlaying(nonCombatAnimationClips[2])) { animancer.CrossFade(nonCombatAnimationClips[2]); } } else if (!animancer.IsPlaying(nonCombatAnimationClips[0])) { animancer.CrossFade(nonCombatAnimationClips[0]); } }
/************************************************************************************************************************/ protected void Update() { // W or UpArrow = 1. // S or DownArrow = -1. // Otherwise 0. var movement = Input.GetAxisRaw("Vertical"); if (movement != 0) { PlayMove(); // Since we don't have animations for moving backwards, just use the input as their speed so that // moving backwards simply plays the animation backwards. _Animancer.CurrentState.Speed = movement; // PlayMove could return the AnimancerState it plays, but using the CurrentState saves a bit of effort. } else { // If we aren't moving, return to idle. _Animancer.CrossFade(_Idle); } }
public void HandleAnimations() { if (Networker == null) { return; } // We set these variables here so we can access them at any time in another script // It's basically caching them so things can be a bit more performant Velocity = Networker.GetVelocity(); MoveDirectionXZ = new Vector3(Velocity.x, 0, Velocity.z); MoveDirectionXZRelative = transform.InverseTransformDirection(new Vector3(Velocity.x, 0, Velocity.z)); MoveSpeed = Networker.GetMoveSpeed(); // Upperbody stuff if (Networker.b1.DrawingWeapon) { if (!Anim.IsPlaying(Weapon.DrawWeapon)) { // Set layer weight Anim.GetLayer(1).SetWeight(1); Anim.Stop(Weapon.ReloadWeapon); Anim.CrossFade(Weapon.ReloadWeapon, 0.1f, 1); } } else if (Networker.b1.Reloading) { if (!Anim.IsPlaying(Weapon.ReloadWeapon)) { // Set layer weight Anim.GetLayer(1).SetWeight(1); Anim.Stop(Weapon.ReloadWeapon); Anim.CrossFade(Weapon.ReloadWeapon, 0.2f, 1); } } else if ((Networker.b1.Attacking || Networker.b1.Aiming) && Controller.ActiveWeapon.AimToCamera) { // Set layer weight Anim.GetLayer(1).SetWeight(1); var state = (LinearMixerState)Anim.Transition(Weapon.Aiming, 1); state.Parameter = (float)Networker.nv_Pitch.Value / 100; } else if (Networker.b1.Attacking && !Controller.ActiveWeapon.AimToCamera) { foreach (AttackAnim attackAnim in Weapon.Attack.Attacking) { if (attackAnim.AttackNumber != Networker.nv_AttackNumber.Value) { continue; } if (attackAnim.UseFullbody) { break; } if (!Anim.IsPlaying(attackAnim.Animation)) { // Set layer weight Anim.GetLayer(1).SetWeight(1); Anim.Stop(attackAnim.Animation); Anim.CrossFade(attackAnim.Animation, 0.15f, 1); } break; } } else if (Networker.b1.Aiming && !Controller.ActiveWeapon.AimToCamera) { // Set layer weight Anim.GetLayer(1).SetWeight(1); var state = (LinearMixerState)Anim.Transition(Weapon.Aiming, 1); state.Parameter = 0; } else { // Set layer weight Anim.GetLayer(1).StartFade(0, 0.15f); } // Lowerbody stuff if (Networker.b1.Attacking || Networker.b1.Aiming) { if (!Controller.ActiveWeapon.AimToCamera) { if (Networker.b1.Aiming) { DoBasicMoveLocomotion(); } else { foreach (AttackAnim attackAnim in Weapon.Attack.Attacking) { if (attackAnim.AttackNumber != Networker.nv_AttackNumber.Value) { continue; } transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(0, 0, 0), 18 * Time.deltaTime); if (!attackAnim.UseFullbody) { DoBasicMoveLocomotion(); break; } if (!Anim.IsPlaying(attackAnim.Animation)) { Anim.GetLayer(1).SetWeight(0); Anim.Stop(attackAnim.Animation); Anim.CrossFade(attackAnim.Animation, 0.15f); } break; } } } else if (Controller.ActiveWeapon.AimToCamera) { DoBasicMoveLocomotion(); Vector3 direction = new Vector3(Networker.nv_AimPositionX.Value / 10, Networker.nv_AimPositionY.Value / 10, Networker.nv_AimPositionZ.Value / 10) - Controller.transform.position; Quaternion lookRotation = Quaternion.LookRotation(direction); lookRotation.x = 0; lookRotation.z = 0; transform.rotation = Quaternion.Lerp(transform.rotation, lookRotation, 18 * Time.deltaTime); } } else if (Networker.b2.IsWallJumpFrozen) { transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(0, 0, 0), 18 * Time.deltaTime); Anim.CrossFade(Base.WallJumpFrozen, 0.15f); } else if (Networker.b2.IsWallJumping && Velocity.y >= -4) { var state = (LinearMixerState)Anim.Transition(Base.WallJumping); if (wallJumpX > 6) { Base.WallJumping.State.Parameter = 1; } else if (wallJumpX < -6) { Base.WallJumping.State.Parameter = -1; } else { Base.WallJumping.State.Parameter = 0; } transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(0, 0, 0), 15 * Time.deltaTime); } else if (Networker.b1.IsDodging) { if (Networker.b1.DodgeDirection && !Anim.IsPlaying(Base.DodgeRight)) { Anim.Stop(Base.DodgeRight); // Moving right var state = Anim.CrossFade(Base.DodgeRight, 0.2f); state.Speed = 1.4f; } if (!Networker.b1.DodgeDirection && !Anim.IsPlaying(Base.DodgeLeft)) { Anim.Stop(Base.DodgeLeft); // Moving left var state = Anim.CrossFade(Base.DodgeLeft, 0.2f); state.Speed = 1.4f; } transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(0, 0, 0), 15 * Time.deltaTime); } else if (Networker.b2.IsWallSlammed) { if (!Anim.IsPlaying(Base.WallSlammed)) { Anim.CrossFade(Base.WallSlammed, 0.1f); } Quaternion to = Quaternion.LookRotation(MoveDirectionXZ); transform.rotation = Quaternion.Lerp(transform.rotation, to, 20 * Time.deltaTime); } else if (Networker.b2.IsKnockingBack) { Anim.CrossFade(Base.KnockingBack, 0.25f); // Rotate Quaternion to = Quaternion.LookRotation(-MoveDirectionXZ); transform.rotation = Quaternion.Lerp(transform.rotation, to, 15 * Time.deltaTime); } else if (Networker.b2.IsFlinching) { } else if (Networker.b2.IsStunned) { } else { if (!Networker.b1.IsSprinting) { DoBasicMoveLocomotion(); } else { transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(0, 0, 0), 20 * Time.deltaTime); Anim.CrossFade(Weapon.Sprint, 0.2f); } } // Update weapon holster/attack/rest if (Networker.b1.IsSprinting || Networker.b1.IsDodging || Networker.b2.IsWallJumpFrozen || Networker.b2.IsWallJumping || Networker.b2.IsStunned || Networker.b2.IsKnockingBack || Networker.b2.IsWallSlammed) { Controller.ActiveWeapon.ActiveSkin.Holster(); } else if (Networker.b1.Attacking || Networker.b1.Reloading) { Controller.ActiveWeapon.ActiveSkin.Hold(); } else { Controller.ActiveWeapon.ActiveSkin.Hold(); } // Update Final IK ModelRefrences.Tilt.weight = Networker.b1.IsSprinting ? 1 : 0; // Animation effects CheckDodge(); //CheckWallJump(); CheckJump(); CheckLand(); CheckSprint(); if (Networker.b1.IsGrounded && !Networker.b1.IsDodging && !Networker.b2.IsKnockingBack && !Networker.b2.IsStunned && !Networker.b2.IsFlinching && MoveDirectionXZ.magnitude != 0.0f && !Networker.nv_Dead.Value && !(Networker.b1.Attacking && !Controller.ActiveWeapon.AimToCamera)) { DoFootstep(); } }