- Home /
Assigning different Damage to different bullets,Assigning different damage for each bullet
Hello!
I am new to Game design, and currently, I am working on a Unity 2D game that has elemental aspects for the enemies as well as the player's attacks(bullets). so far i've made the shooting script and added switch weapon function but i am not sure how to approach adding damage to the different bullets and making the enemies taking damage depending on their elements and the player's bullets.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
public GameObject[] bullets;
private float shootingDelay = 0.35f;
private float cooldownTimer = 0.5f;
public int currentBullet = 0;
void Update()
{
if (Input.GetKeyDown(KeyCode.X))
{
++currentBullet;
if (currentBullet+1 > bullets.Length)
{
currentBullet = 0;
}
}
cooldownTimer -= Time.deltaTime;
if (Input.GetButton("Fire1") && cooldownTimer <= 0)
{
cooldownTimer = ShootingDelay;
Debug.Log("Pew!");
Instantiate(bullets[currentBullet], transform.position, transform.rotation);
}
}
}
Thanks in Advance, Any and every bit of help/advice would be appreciated, or any way to set me on the right path to tackle this. :)
Answer by mansoor090 · Feb 20, 2019 at 11:20 AM
There are two ways , i would approach
1- Use RayCast2D and give damage to enemy/player, Set PlayerHealth variable in the script and keep decreasing until it dies (on being attacked) 2- Else use a rigidBody on bullet (gameOject) and a collider Make a new script for Bullet Damage
public class BulletDamage : MonoBehaviour {
public float damageAmount;
public RigidBody rb;
void OnEnable(){
//make sure u uncheck use gravity
rb.velocity = some direction using vector2
}
void OnTriggerEnter2D(Collider2D collider){
//compare game tag and call give damage function....
//giving each bullet some damage is out of normalperson. you can set damageAmount according to Player/Enemy
}
}