hey people, i have this question of scripting i cant solve, hope ou can help me!
I am making a game that consists of a naval battle, and when shooting toward a boat, i wanted to make it explode and then sank, someone has an idea how can I do this?
i already have my script to shoot bullets, and a collider but it is no good…
I’m not a code Guru so I wont be able to provide you with the code to do this without testing it but If you just need a step in the right direction this is what I would do:
Make a function that handles the boats health and upon collision with the bullet have it subtract an amount from the health total
When the total reaches zero, instantiate a particle system for the explosion at the position of the boat, have an animation that sinks the boat, trigger the animation after the explosion and then destroy the game object to get rid of the boat from the game.
Maybe someone else can post some code but this is how I would handle the boat.
Tnx a lot that is a great idea! but i’m still with a problem, i’m not also a big programmer!
I’ve thought about doing this myself, but the problem is to add a score, it would be fantastic in my game, and I’ve seen some toturiais to see if I can make score, but so far I’ve seen toturiais of version 2.9 of unity and it’s predictable, the language has evolved and the code is no longer the same! do you know any toturial that can teach me how to do this?
Ps: plz someone answer me that, because I’m in a race against time to deliver this project until 22 of this month!
I wouldn’t say that this is the best way to do it, but this should be some easy to understand code. Have this in a script attached to your boat.
//You will assign these two transforms in the inspector
//The first will refer to a prefab
//The second one to a "controller" object in your scene.
var explosion : Transform;
var pointMaster : Transform;
//Call this function when the boats health reach's zero
function Sink () {
animation.CrossFade("Sink");//Or whatever your sinking animation is called.
Destroy(GameObject,timeForBoatToSink);
//Replace timeForBoatToSink with the time it takes for your animation to play.
pointMaster.GetComponent(pointController).points+=1;
}
Then on your pointMaster object you have the following script:
var points = 0; //This will make it an int.
//If you want it to be a float, set it equal to 0.0
//You could also add a part in OnGUI to display the current number of points.
//And that's all you need.
Finally you will want to create an explosion gameobject. You will give it a particle system that you like, the following script, then assign it to a prefab and that prefab to the above script. This following script will get rid of the explosion gameObjects when they have filled their use.
var lifeTime = 3.0f;//This is how long it takes your explosion to "play out"
function Start () {
Destroy(gameObject,lifeTime);
}
There are better ways to do this, but it should work and be easy to understand. Note, I did not test this script, so there might be typos.