So in this game im creating (im trying to make the last level in the proto~type) I have a boss. And I also have turrets. Now the turrets blow after one hit… but im trying to make the boss have like 5-10 hits or so. Is there a script I could use. I’m look for something like,
[The following is not real script: just a simple example]
If hit by (this object)
Boss1 = dead
But I need it to have about 10 lives. The main character shoots a little attack that I use for everything else. But when I try to make more lives it doesn’t work. Is there a script for this?
This is clearly a core part of your game. The thing is, you know how to handle collisions. Instead of just killing the object outright, have it decrement the health by 1 and then check the health and if the health is at 0, destroy the object normally. Easy peasy.
What kind of script would this look like… do you think that you could post a script that looks about the same as what your saying, I can always tweak it a bit if I need to.
Well, money is no object to me… As a rough draft here ya go…
var BossHealth : Int = 10;
function Update () {
if (hit by object) {
BossHealth--; (or BossHealth -= 1, either would work fine)
}
if (BossHealth == 0) {
Destroy(Boss);
}
}
Tiger, it is less about the money and more about showing a real commitment to learning. Your sample is an extremely simple piece of code, which basically makes code out of my narrative instructions. If people aren’t willing to make a serious effort they will not get better. If they have no code to post, it is improbable that they have actually tried. Plenty of people post some atrocious code on these forums, but it shows effort and gives people a starting point for clearing up confusion.
I sarted unity 2 days ago. I don’t script very well -.- and the only way to learn is to be teached. Ive only had 2 days worth of scripting in my head. Which doesn’t even dent what scripts can do. So don’t judge someone b4 you know them -.-
Its ok ^^ I hate how cases matter in this im used to the HTML formats and almost any other type of code were it doesn’t matter?
Edit: I have another error, don’t know why it should be fine.
So I have another error and here is my script out of what you gave me.
var BossHealth : int = 10;
function Update ()
{
if(hit.gameObject.tag == "wormProjectile")
{
BossHealth--; //(or BossHealth -= 1, either would work fine)
}
if (BossHealth == 0)
{
Destroy(gameObject);
Destroy(hit.gameObject);
}
}
In my script here: if (hit.gameObject.tag == “wormProjectile”): I get a weird error saying that hit is an unknown identifier:
Unknown identifier: ‘hit’.
It also does this error down after the destroy. I use this script code in some of my other scripts and everything works fine. Why does it not work here?
In Unity there is a menu option labeled “Help” this is located on the menu bar across the top. Click on the “Help”, now in this list is a menu option labeled “Scripting Manual”, let this be your new best friend. Click on this option and once it opens up you will need to do a search. Since you are new to Unity, your search for this operation should be for the word “oncollisionenter” put that word in the search box above the word “Menu” and hit enter. Now, to do what you want, you need to understand about rigid bodies and colliders.
You will want to learn about colliders and how they work with regards to one thing hitting another, in this case, your bullet hitting the boss. Thus the bullet “collides with” the boss. Look through that code and work out proper collider calls
var BossHealth : int = 10;
function Update ()
{
if (BossHealth == 0)
{
Destroy(gameObject);
Destroy(hit.gameObject);
}}
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject.tag == "wormProjectile")
{
BossHealth--; //(or BossHealth -= 1, either would work fine)
}
}
And im looking at the search atm
Edit:
Also I have tried a couple of the different things found with your search… but non of these fix the error of the hit not being recognized. And its only for this script.
Let Debug.Log become your next best friend.
To see if your trigger is setup correctly or if your colliders is setup correctly, in your code where you decrement the health, put a Debug.Log(“I hit it!”); or something, in the beginning, you will want to use Debug.Log like you would use catchup on fries, or salt on fries, or if you don’t like fries, then water on plants, use it as much as possible to understand what is going on in the code then remove the logging after you have made sure events are happening when you think they should be.
Edit:
When you click on your boss in the scene, what is the collider attached to the boss? Is the “Is Trigger” box checked?
Here is a hint, (headed to bed so won’t be checking the threads again until late tomorrow), this code is code I have on a bunker in one of my games, my bunker has 10 health, this is C# script, not java script.
using UnityEngine;
using System.Collections;
public class pBunker : MonoBehaviour {
GameObject sManager;
public AudioClip bunkerSND;
public int lifeSpan=10;
void Start () {
sManager=GameObject.Find("ScriptManager");
}
void Update () {
}
void OnCollisionEnter(Collision obj)
{
lifeSpan-=1;
if(lifeSpan<0)
Destroy(gameObject);
}
public void adjustLife(GameObject inObj)
{
if(inObj.GetInstanceID()==this.GetInstanceID())
{
lifeSpan-=1;
if(lifeSpan<0)
Destroy(gameObject);
}
}
}
Ignore parts of that code, not relevant to your situation, but leaving the full code in tact for this post. Please note, that the object in question is a defense bunker, the bunker is set with a box collider but is not set with trigger, so I am not using “OnTriggerEnter” but I am using “OnCollisionEnter”, hope this hint helps.
Yes I do have it set as a trigger and all I wan’t to know is why it says: Assets/Levels/Level5/Boss Health.js(7,9): BCE0005: Unknown identifier: ‘hit’. : because I use the same code in almost every other script I have.
var BossHealth : int = 10;
function Update ()
{
if (BossHealth == 0)
{
Destroy(gameObject);
Destroy(hit.gameObject);
}}
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject.tag == "wormProjectile")
{
BossHealth--; //(or BossHealth -= 1, either would work fine)
}
}
Your problem is that “hit” won’t be defined in your Update function.
Think of a function like a message sent by Unity to your script. (In fact, that IS how it works.)
For Update, you’re listening to Unity saying “OK, it’s a new frame! What do you want to do?”
For OnTriggerEnter, it’s closer to “Hey, something just touched this object, here it is”
I’ll give you a huge hint: you’re destroying the “hit” object in Update(). Unity has no clue what “hit” means outside of OnTriggerEnter. Even if your code did work, it would only destroy the projectile that actually killed the boss, and not the ones that just damage it.
Another hint, remove the Destroy(hit.gameObject) from this code, and use a detect collision or hit in the bullet script so that when it hits something else, it destroys itself. Don’t have something destroy it, have it take care of itself.