• 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 /
avatar image
0
Question by acarlsoma · Nov 02, 2020 at 10:07 AM · damageenemy aihealthprojectilesc #

I cant get my projectiles to do damage or keep them from going through my player

see i been at this for 3 days and im trying to get my enemy to hurt me. i got him to shoot sphere projectiles but cant get it to do damage at all. this is all the codes i have.`

using System; using UnityEngine; using UnityEngine.AI;

public class enemyhurtsyou : MonoBehaviour { public NavMeshAgent agent;

 public Transform player;

 public LayerMask whatIsGround, whatIsPlayer;

 public float health;
 //patroling
 public Vector3 walkPoint;
 bool walkPointSet;
 public float walkPointRange;

 //attacking you fuck
 public float timeBetweenAttack;
 bool alreadyAttacked;
 public GameObject projectiles;

 internal void takeDamage(float damage)
 {
     throw new NotImplementedException();
 }

 //states
 public float sightRange, attackRange;
 public bool playerInSightRange, playerInAttackRange;

 private void Awake() 
 {
     player = GameObject.Find("player").transform;
     agent = GetComponent<NavMeshAgent>(); 
 }

 private void Update()
 {
     //checxk for sight and attack range
     playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
     playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);


     if (!playerInSightRange && !playerInAttackRange) Patroling();
     if (playerInSightRange && !playerInAttackRange) ChasePlayer();
     if (playerInSightRange && playerInAttackRange) AttackPlayer();
 }

     private void Patroling()
     {
         if (!walkPointSet) SearchWalkPoint();

         if (walkPointSet)
             agent.SetDestination(walkPoint);

         Vector3 distanceToWalkPoint = transform.position - walkPoint;

         //walkpoint reached
         if (distanceToWalkPoint.magnitude < 1f)
             walkPointSet = false;

     }

     private void SearchWalkPoint()

     {
         //calculate random point in range
         float randomZ = UnityEngine.Random.Range(-walkPointRange, walkPointRange);
         float randomX = UnityEngine.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()
     {
         //make sure enemy doesnt move
         agent.SetDestination(transform.position);

         transform.LookAt(player);

         if (!alreadyAttacked) 
         {
             Rigidbody rb = Instantiate(projectiles, 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), timeBetweenAttack);
         
         }
     }

     private void ResetAttack()
     {
         alreadyAttacked = false;

     }
 public void TakeDamage(int damage) 
 {
     health -= damage;

     if (health <= 0) Invoke(nameof(DestroyEnemy), .5f);
 
 
 
 }
 private void DestroyEnemy() 
 {
     Destroy(gameObject);
 
 }

 private void OnDrawGizmosSelected() 
 
 {
     Gizmos.color = Color.red;
     Gizmos.DrawWireSphere(transform.position, attackRange);
     Gizmos.color = Color.yellow;
     Gizmos.DrawWireSphere(transform.position, sightRange);
 
 
 }




}

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerhealth : MonoBehaviour
 {
     public int maxHealth = 100;
     public int currentHealth;
 
     public HealthBar healthBar;
 
    
 
     // Start is called before the first frame update
     void Start()
     {
         currentHealth = maxHealth;
         healthBar.SetMaxHealth(maxHealth);  
     }
 
     internal void takeDamage(float damage)
     {
         throw new NotImplementedException();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space)) 
         {
             TakeDamage(5);
         }
 
         if (currentHealth <= 0) 
         {
             Destroy(this.gameObject);
         }
     
     }
 
   
 
 
     void TakeDamage(int damage) 
     {
         currentHealth -= damage;
 
         healthBar.SetHealth(currentHealth);
     }
 }
 

 using System.Collections.Generic;
 using UnityEngine;
 
 public class bullet : MonoBehaviour
 {
     public float lifeTime = 3;
 
 
     private void OnTriggerEnter(Collider other) 
     {
         print("hit" + other.name + "!");
         
     }
 
 
 }

im super new to this so im very lost. please help me on this.d

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Odinwastaken · Nov 02, 2020 at 11:16 AM

Just put this in the bullet script:

 void OnTriggerEnter2D (Collider2D collision)
 {
     if (collision.tag == "Player")
     {
         collision.GetComponent<Player>().TakeDamage(damage);
         Destroy(gameObject);
     }
 }

If this dosen't work make sure that the player has the Player tag. If you got any questions feel free to ask.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

140 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

Related Questions

Enemy health drops but not if first enemy lives, how do i change that? 1 Answer

rigidbody enemy goes only forward, no damage delay. 2 Answers

One enemy triggers all the enemies 2 Answers

how to make enemy damage player? 1 Answer

how do i make a script that calculate how much damage did i loss 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges