1
votes

So, every time the skeleton hit the player/character. It won't show the word "HIT!". What did I do wrong?

THE PLAYER IMAGE INSPECTOR

THE SKELETON IMAGE INSPECTOR

THE HIERARCHY IMAGE



SKELETON ENEMY SCRIPT



private Rigidbody2D myBody;

[Header("Movement")]
public float moveSpeed;
private float minX, maxX;
public float distance;
public int direction;

private bool patrol, detect;

private Transform playerPos;
private Animator anim;

[Header("Attack")]
public Transform attackPos;
public float attackRange;
public LayerMask playerLayer;
public int damage;

//sound

VOID AWAKE

void Awake()
{
    anim = GetComponent<Animator>();
    playerPos = GameObject.Find("George").transform;
    myBody = GetComponent<Rigidbody2D>();
}

VOID START

private void Start()
{
    maxX = transform.position.x + (distance);
    minX = maxX - distance;

    //if (Random.value > 0.5) direction = 1;
    //else direction = -1;
}

VOID UPDATE

void Update()
{
    if (Vector3.Distance(transform.position, playerPos.position) <= 4.0f) patrol = false;
    else patrol = true;
}

VOID FIXED UPDATE

private void FixedUpdate()
{
    if (anim.GetBool("Death"))
    {
        myBody.velocity = Vector2.zero;
        GetComponent<Collider2D>().enabled = false;
        myBody.isKinematic = true;
        anim.SetBool("Attack", false);
        return;
    }


    if (myBody.velocity.x > 0)
    {
        transform.localScale = new Vector2(1f, transform.localScale.y);
        anim.SetBool("Attack", false);
    }
    else if
        (myBody.velocity.x < 0) transform.localScale = new Vector2(-1f, transform.localScale.y);


    if (patrol)
    {
        detect = false;
        switch (direction)
        {
            case -1:
                if (transform.position.x > minX)
                    myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
                else
                    direction = 1;
                break;
            case 1:
                if (transform.position.x < maxX)
                    myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
                else
                    direction = -1;
                break;
        }
    }
    else
    {
        if (Vector2.Distance(playerPos.position, transform.position) >= 1.0f)
        {
            if (!detect)
            {
                detect = true;
                anim.SetTrigger("Detect");
                myBody.velocity = new Vector2(0, myBody.velocity.y);
            }
            if (anim.GetCurrentAnimatorStateInfo(0).IsName("Detect")) return;



            Vector3 playerDir = (playerPos.position - transform.position).normalized;

            if (playerDir.x > 0)
                myBody.velocity = new Vector2(moveSpeed + 0.4f, myBody.velocity.y);
            else
                myBody.velocity = new Vector2(-(moveSpeed + 0.4f), myBody.velocity.y);
        }
        else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
        {
            myBody.velocity = new Vector2(0, myBody.velocity.y);
            anim.SetBool("Attack", true);
        }
    }
}

VOID ATTACK

 public void Attack()
    {
        myBody.velocity = new Vector2(0, myBody.velocity.y);

        Collider2D attackPlayer = Physics2D.OverlapCircle(attackPos.position, attackRange, playerLayer);

        if (attackPlayer == null)
        {
            if(attackPlayer.tag == "Player")
            {
                print("Hit!");
                attackPlayer.gameObject.GetComponent<PlayerHealth>().TakeDamage(damage);
            }
        }
    }
    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(attackPos.position, attackRange);
    }


PLAYER HEALTH SCRIPT



VOID AWAKE

 public int health = 100;

    void Awake()
    {

    }

VOID UPDATE

 void Update()
    {
        if (health < 1)
        {
            print("Dead");
        }    
    }

VOID TAKE DAMAGE

 public void TakeDamage(int damage)
    {
        FindObjectOfType<CameraShake>().ShakeItMedium();
        health -= damage;
    }

    private void OnTriggerEnter2D(Collider2D target)
    {
        if(target.tag == "Fireball")
        {
            TakeDamage(25);
        }
    }
2
Please turn this into a minimal reproducible example there is a lot of code there, and other than "its not working" theres too much for people to wade through, and links are generally less approved.BugFinder
I'm still new to this page but I will try doing it! :DSaidus

2 Answers

0
votes

You dont seem to call the "Attack" method in your script.

else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0)
{
    myBody.velocity = new Vector2(0, myBody.velocity.y);
    // calling the attack method, so the physics cast is being made
    Attack();
    anim.SetBool("Attack", true);
}

And dont forget to change this, you are checking if the cast hit nothing instead of something.

// if the cast hit something
if (attackPlayer != null)
{
    if(attackPlayer.tag == "Player")
    {
        print("Hit!");
        attackPlayer.gameObject.GetComponent<PlayerHealth>().TakeDamage(damage);
    }
}
0
votes

While there maybe many other things wrong with that long pile of code:

if (attackPlayer == null) { if(attackPlayer.tag == "Player") { print("Hit!"); attackPlayer.gameObject.GetComponent<PlayerHealth>().TakeDamage(damage); } }

You check if attackPlayer is null and then try do work on it... Im pretty you sure you meant if (attackPlayer != null)...