I have completed the space shooter tutorial successfully, I am not wondering how I can go about making laser weapons hit targets multiple times before they are destroyed? I use a DestroyByContact script which simply destroys as soon as it comes into contact with a target, can this script be modified or will I need a new script with different parameters?
Haven’t looked at the destroybycontact script. But if this script isn’t on your target, you’ll probably need a script on the targets that keeps track of number of hits it can take. A simple int value that you subtract from each time it is hit and then check if it’s value is 0 before destroying will serve this purpose.
Well, provide the DestroyByContact script so we can see what you’re doing.
When you’re hitting the enemy, this is the logic you need:
//when hit by enemy
EnemyObject.GetComponent<HPScript>().HPamount -= DamageAmount;
OP, you can do anything you want too. Describe your functionality better as I’m struggling to understand. Do you want a laser to hit the same target multipe times or a laser beam to be able to hit multiple targets a single time, or something else?
As this seems a different type of ‘attack’, if you want to keep the original and new functionality, you will need to figure out how to decide which one is being used and setup the code accordingly so it uses the right methods. This is a very vague question.
Sorry, i’ll explain exactly what im wanting to do.
For some objects such as large asteroids or certain enemies I want them to receive multiple laser hits before they are destroyed. For a boss fight naturally you would need it to take many hits before it is destroyed, but for a smaller asteroid one or two hits would be good.
This is my destroy by contact script:
using UnityEngine;
using System.Collections;
public class DestroyByContact : MonoBehaviour
{
public GameObject explosion;
public GameObject playerexplosion;
public int scoreValue;
private GameController gameController;
void Start()
{
gameController = GameObject.FindObjectOfType<GameController>();
if (gameController == null)
{
Debug.Log("Cannot Find 'GameController' script");
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Boundary" || other.tag == "Enemy")
{
return;
}
if (explosion != null)
{
Instantiate(explosion, transform.position, transform.rotation);
}
if (other.tag == "Player")
{
Instantiate(playerexplosion, other.transform.position, other.transform.rotation);
gameController.GameOver();
}
gameController.AddScore(scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
}
So before Destroy(other.GameObject), apply the logic I showed.
Gotcha, that should be easy enough then. You are going to want something like a ‘health’ system. That based on the number of hits or damage that the object receive, it will be destroyed.
I would look into Unity Learn. Honestly, most of the tutorials there implement a health system. Find one that looks like your game genre if that’s there and work through that. Then tweak it for your needs.
Basically you are going to add to add a integer to each object, e.g. ‘public int health = 10’. Then I believe what you want to do is inside your OnEnterTrigger if it’s enemy, decrease the health variable by whatever amount you want then if it’s less or equal to 0, destroy the object. That is the basic high level overview. Look at the link I provided to see some code and see it in action.