So ive been working on a game or lets say practicing on the Space Shooter game the unity provided. and what i am trying to do is instead of just 1 shot 1 kill enemies i want to put and health and damage system.
If anyone can give me a start and i can play around with it, i would appreciate.
please keep in mind that and a complete beginner so please bare with me!
Thanks
Good morning and thank for replying got worried there for a sec. I have the bullet but it just destroys the target instantly i want the bullet to decrease the health… and vise versa for enemies!
Alright, the script above does what you want. When the enemy touches the bullet , enemy’s health is decreased by bulletStrength, in this case 20; so you would need 5 bullets to completely kill the enemy;
Paste this code into your enemy
int health = 100;
void OnCollisionEnter(Collider bullet){
if(bullet.gameObject.tag == "Bullet"){
//Bullet strength is 20
health -= 20;
}
if(health <= 0){
Destroy(this.gameObject);
}
}
Ok well i tried to play around with other scripts form other sample projects but its still not working
This is the Health Scripts
using UnityEngine;
namespace CompleteProject
{
public class EnemyHealth : MonoBehaviour
{
public int startingHealth = 100; // The amount of health the enemy starts the game with.
public int currentHealth; // The current health the enemy has.
void Awake ()
{
// Setting the current health when the enemy first spawns.
currentHealth = startingHealth;
}
public void TakeDamage (int amount, Vector3 hitPoint)
{
// Reduce the current health by the amount of damage sustained.
currentHealth -= amount;
// If the current health is less than or equal to zero...
if(currentHealth <= 0)
{
}
}
}
}
and this is the damage script
using UnityEngine;
using UnitySampleAssets.CrossPlatformInput;
namespace CompleteProject
{
public class PlayerShooting : MonoBehaviour
{
public int damagePerShot = 20; // The damage inflicted by each bullet.
public float timeBetweenBullets = 0.15f; // The time between each shot.
float timer; // A timer to determine when to fire.
Ray shootRay; // A ray from the gun end forwards.
RaycastHit shootHit; // A raycast hit to get information about what was hit.
int shootableMask; // A layer mask so the raycast only hits things on the shootable layer.
void Update()
{
// If the Fire1 button is being press and it's time to fire...
if (Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0) {
// ... shoot the gun.
Shoot ();
}
}
void Shoot ()
{
// Reset the timer.
timer = 0f;
// Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
// Perform the raycast against gameobjects on the shootable layer and if it hits something...
if(Physics.Raycast (shootRay, out shootHit, shootableMask))
{
// Try and find an EnemyHealth script on the gameobject hit.
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
// If the EnemyHealth component exist...
if(enemyHealth != null)
{
// ... the enemy should take damage.
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
}
}
}
}
i dont know if they are compatible… ive spend days on this but no clue!
File names: EnemyHealth and PlayersShooting should have same file name as their class names. Probably not the problem, if you’re not getting any errors.
Layer masks: in PlayerShooting there’s raycast on shootableMask layer, which is a number what layer the game is checking for the raycast. Problem is, that the shootableMask is not actually set to anything in that script. So first you need to have a layer for objects you want to be able to shoot. You can create a new layer named “Shootable” in Unity and set your enemies to be in that layer. Then from the PlayerShooting script set the shootableMask to be the same number, as is your “Shootable” mask in Unity inspector. This way the raycast knows to check if it hits to a object on that Shootable layer.
Two ways to set: set the int shootableMask be the same as layer mask number in Unity.
And even better way (won’t break if you mess around with layers later)
Put this into PlayerShooting script, it will find layer named “Shootable” and set the shootableMask value to right one.
you can’t use a layermask without providing a distance, they are both optional parameters, they are both numeric values. If you only provide one thinking it’s the mask, the int bitmask will be implicitly converted to a float and used as a distance as it’s the 3rd parameter passed into the function.