using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootBullet : MonoBehaviour
{
public float bulletVelocity;
public GameObject currentBulletPrefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Bullet();
}
void Bullet()
{
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)((worldMousePos - transform.position));
direction.Normalize();
// Creates the bullet locally
GameObject bullet = (GameObject)Instantiate(
currentBulletPrefab,
transform.position + (Vector3)(direction * 0.5f),
Quaternion.identity);
// Adds velocity to the bullet
bullet.GetComponent<Rigidbody2D>().velocity = direction * bulletVelocity;
StartCoroutine(DestroyBullets()); // Destroys bullets
IEnumerator DestroyBullets()
{
yield return new WaitForSeconds(3);
Destroy(bullet);
}
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.transform.tag == "Enemy")
{
collision.gameObject.GetComponent<Enemy>().TakeDamage(5);
}
}
}
#Bullet not doing damage
1 messages · Page 1 of 1 (latest)
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public float maxHealth;
public float currentHealth;
BoxCollider2D hitbox;
void Update()
{
hitbox = GetComponent<BoxCollider2D>();
}
public void TakeDamage(int damageAmount)
{
currentHealth -= 5;
print(currentHealth);
}
}
wait
when i hit my Enemy (that has the Enemy layer) it doesnt do anything
Check that both collider do not have the trigger option enabled
and that one of them has a rigidbody
I actually had isTrigger enabled on the enemy, but disabling it still doesnt work, both of them have a rigidbody2d
rigidbody2d will not work with 3D collider and 3D events
you need to use the 2D events for that
are you using collider or collider 2D?
box collider 2d for the enemy
sorry i cant figure out what the issue is, im way too tired lol, ill try to check on it tomorrow, thanks for all your help today anyway
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public float maxHealth;
public float currentHealth;
Collider2D hitbox;
void Update()
{
hitbox = GetComponent<Collider2D>();
}
public void TakeDamage(int damageAmount)
{
currentHealth -= 5;
print(currentHealth);
}
}
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootBullet : MonoBehaviour
{
public float bulletVelocity;
public GameObject currentBulletPrefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Bullet();
}
void Bullet()
{
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)((worldMousePos - transform.position));
direction.Normalize();
// Creates the bullet locally
GameObject bullet = (GameObject)Instantiate(
currentBulletPrefab,
transform.position + (Vector3)(direction * 0.5f),
Quaternion.identity);
// Adds velocity to the bullet
bullet.GetComponent<Rigidbody2D>().velocity = direction * bulletVelocity;
StartCoroutine(DestroyBullets()); // Destroys bullets
IEnumerator DestroyBullets()
{
yield return new WaitForSeconds(3);
Destroy(bullet);
}
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.transform.tag == "Enemy")
{
collision.gameObject.GetComponent<EnemyHealth>().TakeDamage(5);
Debug.Log("Hit");
}
}
}
i still cant get it to work, it doesnt print nor make the enemy take damage
Take the Bullet method out of Update.
Simplify the destruction: don't use a coroutine, use Destroy(bullet, 3);
The OnCollisionEnter method must be on the bullet
- Either the bullet or what you're hitting must have a 2D Rigidbody
- Both the bullet and what you're hitting must have a 2D, non-trigger Collider
ohh right lol thank you, i didnt read enough into the destroy doc and didnt realise i can do that
Next step: follow Niktu's advice. The bullet itself must have that OnCOllisionEnter code, not whatever fires the bullet
okay, now it knows when the enemy is hit, but im getting this error, from what i understand its something to do with the ```cs
GetComponent<Enemy>()
You get the error because class Enemy is not a MonoBehaviour - you sure you have the right class name?
No you don't. From the screenshots above you should be getting EnemyHealth instead
It is recognized as a class, though.
i just fixed the issue but im not sure if my code is efficient
Bullet code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.transform.tag == "Enemy")
{
collision.gameObject.GetComponent<Enemy>().TakeDamage(5);
Debug.Log("Hit");
}
}
}
Enemy code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float maxHealth;
public float currentHealth;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void TakeDamage(int damageAmount)
{
currentHealth -= 5;
print(currentHealth);
if(currentHealth <=0)
{
Destroy(gameObject);
}
}
}
Do you still get the error now?
Is the script saved?
no, everything is working now, i can kill the enemy by hitting it a few times (depending on its health)
Good stuff.
I didnt have a Bullet nor Enemy script before, I only had a ShotBullet and EnemyHealth script, I was actually wondering when and what you have to script in to your enemy / player but now I kinda got it
Alright. You can probably close the thread then. Great success :)
thank you very much for the help everyone ☺️
one last thing, when my enemy spawns i get these 3 errors, im not sure what to fix cause when i try to find the problem it just doesnt show in visual studio
using System.Collections;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public GameObject enemyPrefab;
public int enemyCount;
public float minX;
public float maxX;
public float minY;
public float maxY;
void Start()
{
StartCoroutine(SpawnEnemies());
}
IEnumerator SpawnEnemies()
{
do
{
yield return new WaitForSeconds(3);
Vector2 randomPosition = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
Instantiate(enemyPrefab, randomPosition, Quaternion.identity);
enemyCount++;
}
while (enemyCount < 10);
}
}