- Home /
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
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613015022im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
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
Enemy health drops but not if first enemy lives, how do i change that? 1 Answer
Health Regeneration 2 Answers