H
H
Hiniks YouTube2022-04-13 21:01:21
C++ / C#
Hiniks YouTube, 2022-04-13 21:01:21

How can I make the animation (Jump_Attack) fire when the Attack() function is running and CheckingGround() is false?

You need to make the animation work when the Attack() function is running and CheckingGround() is false

PLAYER SCRIPT

public class Player : MonoBehaviour
{
public Rigidbody2D rb;
public Vector2 moveVector;
public float speed = 3.3f;
public float jumpForce = 5.5f;
public Animator anim;
public SpriteRenderer sr;
public bool onGround;
public Transform GroundCheck;
public float checkRadius = 0.5f;
public LayerMask Ground;
public static Player Instance { get; set; }
[SerializeField] private int lives = 5;
private Vector3 pos;
[SerializeField] float KickRange;
[SerializeField] GameObject KickPoint;

void Start()
{
rb = GetComponent();
anim = GetComponent();
sr = GetComponent();
_combatPlaye = GetComponent();
Instance = this;
}

void Update()
{
if (transform.position.y < -17f)
Debug.Log("DEad");

walk();
Jump();
Flip();
CheckingGround();
stop();

}

//WALK

void Walk()
{
moveVector.x = Input.GetAxis("Horizontal");
anim.SetFloat("MoveX", Mathf.Abs(moveVector.x));
rb.velocity = new Vector2(moveVector.x * speed, rb.velocity.y);
}

//JOB

void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && onGround)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}

void Flip()
{
if (moveVector.x > 0)
{
sr.flipX = false;
}
else if (moveVector.x < 0)
{
sr.flipX = true;
}
}

public void CheckingGround()
{
onGround = Physics2D.OverlapCircle(GroundCheck.position, checkRadius, Ground);
anim.SetBool("onGround", onGround);
}

COMBAT SCRIPT

public class combatPlaye : MonoBehaviour
{

public Animator anim;
public Transform AttackPoint;
public float attackRange = 0.5f;
public LayerMask enemyLayers;
public Vector2 moveVector;

public int attackDamage = 40;
public float attackRate = 2f;
float nextAttackTime = 0f;
private Player_Player;

void Start()
{
_Player = GetComponent();
}

void Update()
{
if (Time.time >= nextAttackTime)
{
if (Input.GetKeyDown(KeyCode.F))
{
Attack();
nextAttackTime = Time.time + 1f / attackRate;
}
}

}

public void Attack()
{
// ATTACK ANIMATION
anim.SetTrigger("Attack");
//ATTACK
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(AttackPoint.position, attackRange, enemyLayers);
//WHO ARE DAMAGED
foreach (Collider2D enemy in hitEnemies)
{
enemy.GetComponent().TakeDamage(attackDamage);

}

}

void OnDrawGizmosSelected()
{
if (AttackPoint == null)
return;

Gizmos.DrawWireSphere(AttackPoint.position, attackRange);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill, 2022-04-18
@kirilla39

Tried to check the value of CheckingGround () in Attack ( ) and from this choose which animation to call?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question