private void SyncAnimations(AnimationSyncData syncData) { if (syncData.Jump) { m_Animator.SetTrigger("Jump"); } if (syncData.HitGround) { m_Animator.SetTrigger("Hit Ground"); } m_Animator.SetBool("Walking", syncData.Walking); m_Animator.SetFloat("Vertical Speed", syncData.Velocity.y); if (syncData.Input.x < 0) { // Flip sprite to face left when moving left transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x) * -1, transform.localScale.y, transform.localScale.z); } else if (syncData.Input.x > 0) { transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z); } }
// Add a synced animation to the sync dictionary public void AddSyncedAnimation(int layer, AnimationState state, AT_Animation animNode ) { if (state == null) { Debug.Log("Adding null animation state for syncing"); return; } // If the layer doesn't exist in the dictionary yet, add it if (!m_syncedAnimations.ContainsKey(layer)) { m_syncedAnimations.Add(layer, new List<AnimationSyncData>()); } // Create the new AnimationSyncData object for this animation AnimationSyncData data = new AnimationSyncData(); data.m_state = state; data.node = animNode; // Add in the new state to the sync layer m_syncedAnimations[layer].Add(data); // Calculate new speed factors for layer RecalculateSpeedVariablesForLayer(layer); }
private void FixedUpdate() { float horizontal = Input.GetAxisRaw("Horizontal"); float vertical = Input.GetAxisRaw("Jump"); Vector2 velocity = new Vector2(m_Rigidbody.velocity.x, m_Rigidbody.velocity.y); AnimationSyncData syncData = new AnimationSyncData { HitGround = false, Jump = false }; // Check for ground below the player Vector2 bottom = new Vector2(transform.position.x + m_Collider.offset.x, transform.position.y + m_Collider.offset.y - m_Collider.bounds.extents.y); Vector2 boxSize = new Vector2(m_Collider.bounds.extents.x * 2 * 0.95f, m_GroundCheckDistance); Collider2D collider = Physics2D.OverlapBox(bottom, boxSize, 0, m_GroundLayer); bool wasGrounded = m_Grounded; m_Grounded = collider != null; if (!wasGrounded && m_Grounded) { // Just hit the ground after falling m_Animator.SetTrigger("Hit Ground"); syncData.HitGround = true; } m_Animator.SetBool("Walking", horizontal != 0 && m_Grounded); syncData.Walking = horizontal != 0 && m_Grounded; // Move horizontally if player is pressing a button, otherwise just maintain current horizontal velocity if (horizontal != 0) { velocity.x = horizontal * m_GroundSpeed; if (horizontal < 0) { // Going left, flip sprite to face left transform.localScale = new Vector3(-1 * Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z); } else if (horizontal > 0) { // Going right, flip sprite to face right transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z); } } else if (!m_Grounded) { // When not giving input and not in the air apply air drag so the player doesn't sideways coast forever velocity.x = velocity.x * (1 - m_AirDrag); } m_Animator.ResetTrigger("Jump"); // Jump if grounded if (vertical != 0 && m_Grounded && !m_JumpStillPressed) { velocity.y = vertical * m_JumpSpeed; m_JumpStillPressed = true; m_Animator.SetTrigger("Jump"); syncData.Jump = true; } else if (vertical == 0) { m_JumpStillPressed = false; } m_Animator.SetFloat("Vertical Speed", velocity.y); syncData.Velocity = velocity; syncData.Input = new Vector2(horizontal, vertical); m_AnimationSyncEvent.Invoke(syncData); m_Rigidbody.velocity = velocity; }