- Home /
The question is answered, right answer was accepted
Does no damage.
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);
}
}
jfc no one is gonna give you an answer. When you provide code that is really long, you must add comments! Additionally, video or images of your game running with the issue helps dramatically. No one wants to read this.
I recommend that you add a debug message when you invoke the TakeDamage
method to make sure that it is actually called. If it is not, then you probably have a problem with the way you are getting the enemy object. For each line of code in the functions that matter, add debug messages to make sure that everything is running as it should.
U have right. I added debug msg and it shows correct. Issue is the current Health doesn't want calculate damage. Photo with Inspector debug below reply :)
Answer by TechnicPyro · Oct 17, 2020 at 08:38 AM
From what I see the code that handles the damage for the enemy looks fine. The only thing I can think of is how the enemy is being identified as an enemy by the game.
I see you are using LayerMasks in the player script method Attack() to determine if an enemy is hit. Is it possible you forgot to put the enemy objects on the correct layer? If that's not the case could you also include screen shots of the inspector view of the enemy game object?
Layer is set corectly. Shure, ss here : https://imgur.com/a/LapS63D
Stupid me. I forgot to change tha attack value on player object ;D
Hopefully that works, but if it doesn't I think it may have to do with the foreach loop in the Attack() method of the player scripts.
What I think it might be is you are referring to the collider and trying to get the script component from it when you need access to the transform to access the script.
You are doing this:
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
but should be doing this:
enemy.transform.GetComponent<Enemy>().TakeDamage(attackDamage);
Again, hopefully changing the attack value worked for you, but while I was testing I couldn't get the compiler to be happy with me when I used GetComponent directly on a collider.
Follow this Question
Related Questions
how to make enemy damage player? 1 Answer
Enemy AI Movement To A Target(Player) In A 2D Game 3 Answers
NavMeshAgent does not follow player 0 Answers
How do I stop the enemy ai from following the player after it dies on unity3d properly in C# 0 Answers
How can I move the A* Pathfinding Grid Graph with the camera in a endless runner game? 1 Answer