Yo.
This is my scripts, one for player, second one for the enemies. The deal is, i cannot figure it out why i does no dealing damage. In the dialog log i don’t see any problems.
Ideas?
Player script :
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
public int attackDamage = 1;
public float speed = 6;
public float gravity = -9.81f;
public float jumpHeight = 3;
public float attackRange = 0.5f;
public float groundDistance = 0.1f;
public float turnSmoothTime = 0.1f;
public CharacterController controller;
public Transform cam;
public HealthBar healthBar;
public Transform attackPoint;
public LayerMask whatIsEnemy;
public Transform groundCheck;
public LayerMask groundMask;
float turnSmoothVelocity;
Animator animator;
Vector3 velocity;
bool isGrounded;
bool isWalking;
void Start()
{
animator = GetComponent<Animator>();
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if(direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
isWalking = direction.magnitude >= 0.1f;
if (isWalking && isGrounded)
{
animator.SetBool("walk", true);
}else
{
animator.SetBool("walk", false);
}
if (Input.GetButton("Fire1"))
{
Attack();
}else
{
animator.SetBool("hitting", false);
}
}
void TakeDamage (int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
void Attack()
{
animator.SetBool("hitting", true);
Collider[] hitEnemies = Physics.OverlapSphere(attackPoint.position, attackRange, whatIsEnemy);
foreach(Collider enemy in hitEnemies)
{
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
Debug.Log("HIT");
}
}
void OnDrawGizmosSelected()
{
if(attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
}
Enemy script :
public class Enemy : MonoBehaviour
{
public NavMeshAgent agent;
public Transform player;
public LayerMask whatIsGround, whatIsPlayer;
public Vector3 walkPoint;
public GameObject projectile;
public int maxHealth = 100;
public float walkPointRange;
public float timeBetweenAttacks;
public float sightRange, attackRange;
public bool playerInSightRange, playerInAttackRange;
int currentHealth;
bool walkPointSet;
bool alreadyAttacked;
private void Awake()
{
currentHealth = maxHealth;
player = GameObject.Find("Hero").transform;
agent = GetComponent<NavMeshAgent>();
}
private void Update()
{
playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
if (!playerInSightRange && !playerInAttackRange) Patroling();
if (playerInSightRange && !playerInAttackRange) ChasePlayer();
if (playerInAttackRange && playerInSightRange) AttackPlayer();
}
private void Patroling()
{
if (!walkPointSet) SearchWalkPoint();
if (walkPointSet)
agent.SetDestination(walkPoint);
Vector3 distanceToWalkPoint = transform.position - walkPoint;
if (distanceToWalkPoint.magnitude < 1f)
walkPointSet = false;
}
private void SearchWalkPoint()
{
float randomZ = Random.Range(-walkPointRange, walkPointRange);
float randomX = Random.Range(-walkPointRange, walkPointRange);
walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
walkPointSet = true;
}
private void ChasePlayer()
{
agent.SetDestination(player.position);
}
private void AttackPlayer()
{
agent.SetDestination(transform.position);
transform.LookAt(player);
if (!alreadyAttacked)
{
Rigidbody rb = Instantiate(projectile, transform.position, Quaternion.identity).GetComponent<Rigidbody>();
rb.AddForce(transform.forward * 32f, ForceMode.Impulse);
rb.AddForce(transform.up * 8f, ForceMode.Impulse);
alreadyAttacked = true;
Invoke(nameof(ResetAttack), timeBetweenAttacks);
}
}
private void ResetAttack()
{
alreadyAttacked = false;
}
private void DestroyEnemy()
{
//remember die anim
Destroy(gameObject);
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
//remember hurt anim
if(currentHealth <= 0)
{
DestroyEnemy();
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, attackRange);
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, sightRange);
}
}