Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 17, 2020 at 09:54 AM by budynkamil3 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by budynkamil3 · Oct 16, 2020 at 08:29 PM · playerdamageenemy ai

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);
     }
 }



Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UnityToMakeMoney · Oct 17, 2020 at 07:21 AM 0
Share

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.

avatar image budynkamil3 UnityToMakeMoney · Oct 17, 2020 at 09:48 AM 0
Share

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 :)

1 Reply

  • Sort: 
avatar image
0
Best Answer

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image budynkamil3 · Oct 17, 2020 at 09:39 AM 0
Share

Layer is set corectly. Shure, ss here : https://imgur.com/a/LapS63D

avatar image budynkamil3 · Oct 17, 2020 at 09:54 AM 0
Share

Stupid me. I forgot to change tha attack value on player object ;D

avatar image TechnicPyro budynkamil3 · Oct 17, 2020 at 10:07 AM 0
Share

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

Answers Answers and Comments

173 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges