Hello, I am adding jump animation to game and I have Jump animation play when IsGrounded() = false but when I jump animation will start, In air it will stay on one frame, and then it will play again when player hits the ground, What do I do?
6 Answers
6Could you show the script? So i can help you
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
public class PlayerMovement : MonoBehaviour
{
public Animator animator;
public EssanceManager em;
public Transform attackPoint;
public GameObject popUpPrefab;
public float attackRange = 0.5f;
public int attackDamage = 40;
public LayerMask enemyLayers;
public int damage;
public float attackRate = 3f;
float nextAttackTime = 0f;
private float horizontal;
private float speed = 8f;
private float jumpingPower = 16f;
private bool isFacingRight = true;
private bool inAir;
private bool canDash = true;
private bool isDashing;
private float dashingPower = 150f;
private float dashingTime = 0.2f;
private float dashingCooldown = 1f;
private bool doubleJump;
[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private TrailRenderer tr;
void Update()
{
animator.SetBool("IsDashing", isDashing);
animator.SetFloat("Speed", Mathf.Abs(horizontal));
animator.SetBool("IsJumping", !IsGrounded());
if (isDashing)
{
return;
}
if (Time.time >= nextAttackTime)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Attack();
nextAttackTime = Time.time + 1f / attackRate;
}
}
horizontal = Input.GetAxisRaw("Horizontal");
if(IsGrounded() && !Input.GetKey(KeyCode.W))
{
doubleJump = false;
}
if (Input.GetKeyDown(KeyCode.W))
{
if (IsGrounded() || doubleJump)
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
doubleJump = !doubleJump;
}
}
if (Input.GetKeyUp(KeyCode.W) && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * .5f);
}
if (Input.GetKeyDown(KeyCode.LeftShift) && canDash)
{
StartCoroutine(Dash());
}
Flip();
}
void Attack()
{
animator.SetTrigger("Attack");
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
foreach (Collider2D enemy in hitEnemies)
{
Invoke(nameof(DelayPopUps), .5f);
enemy.GetComponent<BansheeHealth>().TakeDamage(attackDamage);
}
}
private void FixedUpdate()
{
if (isDashing)
{
return;
}
else
{
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}
}
private bool IsGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}
private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
private IEnumerator Dash()
{
canDash = false;
isDashing = true;
float originalGravity = rb.gravityScale;
rb.gravityScale = 0f;
rb.velocity = new Vector2(transform.localScale.x * dashingPower, 0f);
tr.emitting = true;
yield return new WaitForSeconds(dashingTime);
tr.emitting = false;
rb.gravityScale = originalGravity;
isDashing = false;
yield return new WaitForSeconds(dashingCooldown);
canDash = true;
}
void OnDrawGizmosSelected()
{
if (attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
public void DelayPopUps()
{
GameObject popUp = Instantiate(popUpPrefab, attackPoint.position, Quaternion.identity);
popUp.GetComponentInChildren<TMP_Text>().text = attackDamage.ToString();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Collectable"))
{
Destroy(other.gameObject);
em.essenceCount++;
}
}
}
Iam to lazy to test it but try this :
void Update()
{
animator.SetBool("IsDashing", isDashing);
animator.SetFloat("Speed", Mathf.Abs(horizontal));
animator.SetBool("IsJumping", !IsGrounded());
if (isDashing)
{
return;
}
if (Time.time >= nextAttackTime)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Attack();
nextAttackTime = Time.time + 1f / attackRate;
}
}
horizontal = Input.GetAxisRaw("Horizontal");
if (IsGrounded() && !Input.GetKey(KeyCode.W))
{
doubleJump = false;
}
// Jumping handling
if (Input.GetKeyDown(KeyCode.W) && (IsGrounded() || doubleJump))
{
Jump();
}
if (Input.GetKeyDown(KeyCode.LeftShift) && canDash)
{
StartCoroutine(Dash());
}
Flip();
}
void Jump()
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
if (!IsGrounded())
{
doubleJump = true;
}
}
or just do
void Jump()
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
if (!IsGrounded())
{
doubleJump = true;
}
animator.SetTrigger("Jump"); // Trigger the jump animation
}
Ah forget it i have no idea iam sorry
if(IsGrounded() && !Input.GetKey(KeyCode.W))
{
doubleJump = false;
}
if (Input.GetKeyDown(KeyCode.W))
{
if(IsGrounded() || doubleJump)
{
animator.SetTrigger("isJumping");
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
doubleJump = !doubleJump;
}
}
Thank you .Im gonna try to fix it just wait.
– felixklein460