Hello, I just started learning C# and unity. I’m practicing what I’ve learned. What kind of mistake could I have made in this script? My enemy object is moving towards to player but dont attack when colliders collide and when ı shoot and send bullets from my player, the enemy does not affected.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
private Transform playerPos;
private bool isWalk = false;
private bool isAttack = false;
public float speed;
public int health = 3;
public Animator animator;
// Start is called before the first frame update
void Start()
{
playerPos = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
if (transform.position != playerPos.position)
{
Walk();
}
if (health <= 0)
{
Destroy(gameObject);
}
}
void OnCollisionStay(Collision col)
{
if (col.gameObject.CompareTag("Player"))
{
Debug.Log("player col");
Attack();
}
}
/* void OnCollisionExit(Collision col)
{
if (col.gameObject.CompareTag("Player"))
{
isAttack = false;
Walk();
}
} */
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Bullet"))
{
Debug.Log("bullet col");
health--;
}
}
private void Walk()
{
transform.position = Vector2.MoveTowards(transform.position, playerPos.position, speed * Time.deltaTime);
isWalk = true;
WalkAnim();
}
private void Attack()
{
isAttack = true;
AttackAnim();
}
private void AttackAnim()
{
if (isAttack)
{
animator.SetBool("isAttack", true);
}
else
{
animator.SetBool("isAttack", false);
}
}
private void WalkAnim()
{
if (isWalk)
{
animator.SetBool("isWalking", true);
}
else
{
animator.SetBool("isWalking", false);
}
}
}