Sorry for the disturb someone could help? but I have a problem with your tutorial about the spaceshooter .I would add a Health bar UI and every shoot is -10 of damage could you help pls the code are player Code (CSharp): using UnityEngine; using System.Collections; [System.Serializable] public class Boundary { public float xMin, xMax, yMin, yMax; //Screen Boundary dimentions } public class Player_Script : MonoBehaviour { //Public Var public float speed; //Player Ship Speed public Boundary boundary; //make an Object from Class Boundary public GameObject shot; //Fire Prefab public Transform shotSpawn; //Where the Fire Spawn public float fireRate = 0.5F; //Fire Rate between Shots public GameObject Explosion; //Explosion Prefab public GameObject restartDialog; //eND gAME //Private Var private float nextFire = 0.0F; //First fire & Next fire Time private bool dead; void Start () { restartDialog.SetActive(false); } // Update is called once per frame void Update () { //Excute When the Current Time is bigger than the nextFire time if (Time.time > nextFire) { nextFire = Time.time + fireRate; //Increment nextFire time with the current system time + fireRate Instantiate (shot , shotSpawn.position ,shotSpawn.rotation); //Instantiate fire shot audio.Play (); //Play Fire sound } } // FixedUpdate is called one per specific time void FixedUpdate () { float moveHorizontal = Input.GetAxis ("Horizontal"); //Get if Any Horizontal Keys pressed float moveVertical = Input.GetAxis ("Vertical"); //Get if Any Vertical Keys pressed Vector2 movement = new Vector2 (moveHorizontal, moveVertical); //Put them in a Vector2 Variable (x,y) rigidbody2D.velocity = movement * speed; //Add Velocity to the player ship rigidbody //Lock the position in the screen by putting a boundaries rigidbody2D.position = new Vector2 ( Mathf.Clamp (rigidbody2D.position.x, boundary.xMin, boundary.xMax), //X Mathf.Clamp (rigidbody2D.position.y, boundary.yMin, boundary.yMax) //Y ); } //Called when the Trigger entered void OnTriggerEnter2D(Collider2D other) { //Excute if the object tag was equal to one of these if(other.tag == "Enemy" || other.tag == "Asteroid" || other.tag == "EnemyShot") { Instantiate (Explosion, transform.position , transform.rotation); //Trigger That its a GameOver Destroy(gameObject); restartDialog.SetActive(true); } } public void RestartGame() { Application.LoadLevel (Application.loadedLevelName); } public void ExitToMenu() { Application.LoadLevel ("Menu"); } //Destroy Player Ship Object } Code (CSharp): using UnityEngine; using System.Collections; public class EnemyRed_Script : MonoBehaviour { //Public Var public float speed; //Enemy Ship Speed public int health; //Enemy Ship Health public GameObject LaserGreenHit; //LaserGreenHit Prefab public GameObject Explosion; //Explosion Prefab public int scoreValue; //How much the Enemy Ship give score after explosion public GameObject shot; //Fire Prefab public Transform shotSpawn; //Where the Fire Spawn public float fireRate = 0.5F; //Fire Rate between Shots //Private Var private float nextFire = 0.0F; //First fire & Next fire Time // Use this for initialization void Start () { rigidbody2D.velocity = -1 * transform.up * speed; //Enemy Ship Movement } // Update is called once per frame void Update () { //Excute When the Current Time is bigger than the nextFire time if (Time.time > nextFire) { nextFire = Time.time + fireRate; //Increment nextFire time with the current system time + fireRate Instantiate (shot , shotSpawn.position ,shotSpawn.rotation); //Instantiate fire shot audio.Play (); //Play Fire sound } } //Called when the Trigger entered void OnTriggerEnter2D(Collider2D other) { //Excute if the object tag was equal to one of these if(other.tag == "PlayerLaser") { Instantiate (LaserGreenHit, transform.position , transform.rotation); //Instantiate LaserGreenHit ScoreManager.score += scoreValue = 300; Destroy(other.gameObject); //Destroy the Other (PlayerLaser) //Check the Health if greater than 0 if(health > 0) health--; //Decrement Health by 1 //Check the Health if less or equal 0 if(health <= 0) { Instantiate (Explosion, transform.position , transform.rotation); //Instantiate Explosion //Increment score by ScoreValue Destroy(gameObject); //Destroy The Object (Enemy Ship) } } } } Code (CSharp): using UnityEngine; using System.Collections; public class EnemyShot_Script : MonoBehaviour { //Public Var public float speed; //EnemyRed Shot Speed // Use this for initialization void Start () { rigidbody2D.velocity = -1 * transform.up * speed; //Give Velocity to the Enemy ship shot } }
Well, this is a lot of code to read... First off: Have you converted this project to 2D? I see that you are using Rigidbody2D and Collision2D. Now (not really reading this code deeply), let me suggest this, and let's see how we get on... You will need a health statistic for your player (I'm assuming you want your player to have the health bar!), so create a public int playerHealth = 100; or whatever value you want. If your trigger function that would destroy the ship, replace the code where the player ship is being destroyed with code where the playerHealth is reduced by 10 points. To create a health bar, you can use the new UI tools in 4.6. Look at the Lean site and check out the UI modules. One easy way is to use a slider bar, and set the slider bar's value to the health value in the same function that's deducting the health. In that same block of code, you'll probably want to check if the player health is <=0 and then destroy the player at that point.
Could you give me an example of code ?The probelm is that the code is one for everything the code poste above,i read a lot of article about UI health bar but no one is the right for me,i haven't the code to reduce the lives of the player
You have this in your code: Code (csharp): //Called when the Trigger entered void OnTriggerEnter2D(Collider2D other) { //Excute if the object tag was equal to one of these if(other.tag == "PlayerLaser") { Instantiate (LaserGreenHit, transform.position , transform.rotation); //Instantiate LaserGreenHit ScoreManager.score += scoreValue = 300; Destroy(other.gameObject); //Destroy the Other (PlayerLaser) //Check the Health if greater than 0 if(health > 0) health--; //Decrement Health by 1 //Check the Health if less or equal 0 if(health <= 0) { Instantiate (Explosion, transform.position , transform.rotation); //Instantiate Explosion //Increment score by ScoreValue Destroy(gameObject); //Destroy The Object (Enemy Ship) } } } Wouldn't it be similar to this?
I know but It doesn't work I'm Not able To create a void damege To reduce The life At every contact. I could create An internal variable called damege and Then?
I'm confused, then. What you need to do, is find the place where your code destroys the player ship and change that to: Create a health value for the player ship. Reduce the player's health each time it is hit by the amount of damage from the object hitting it. Check the health of the player ship, and if it's less than or equal to 0, destroy the ship. It's been a while since I've looked at the Space Shooter code, and clearly you've modified it for your project so... First: Create a variable to hold your player damage: Code (csharp): public int playerHealth; Then: Look at the code where the player is destroyed... ... if you can't figure that out, post the code where your player is destroyed here. - For what it's worth, that code of yours that you posted and I've reposted above contains most, if not all, of the functionality you will need.
Code (CSharp): using UnityEngine; using System.Collections; public class Destroy : MonoBehaviour { public GameObject Explosion; //Explosion Prefab public GameObject restartDialog; //eND gAME // Use this for initialization void Start () { restartDialog.SetActive(false); } //Called when the Trigger entered void OnTriggerEnter2D(Collider2D other) { //Excute if the object tag was equal to one of these if(other.tag == "Enemy" || other.tag == "Asteroid" || other.tag == "EnemyShot") { { Instantiate (Explosion, transform.position , transform.rotation); //Trigger That its a GameOver Destroy(gameObject); restartDialog.SetActive(true); } } } public void RestartMenu() { Application.LoadLevel (Application.loadedLevelName); } public void ExitToMenu() { Application.LoadLevel ("Menu"); } //Destroy Player Ship Object } This is the code that use to destroy the player. I haven't any code to reduce and invoke the function damage
Right! Now, instead of line 23: Code (csharp): Destroy(gameObject); ... you want to do the deduction for health and the test for dead. You may also want to hold back on line 21 (The instantiate explosion) and keep that for when the player tests true for being dead.
How should I do this I m a noob With csharp.Could you write The string of code what is The deduction for Heath?And how every Bullet is -10 of damage . I m freaking out I m so sorry
Well, did you write the code you've been posting so far? If so, it's not horrible, and you should be able to extend it. If you're using code as a foundation and want to extend it, then here's your learning opportunity! Did you complete the Space Shooter series from beginning to end before trying to modify it? You may be getting ahead of yourself if you have not. To get some help with coding, don't forget the learn section: http://unity3d.com/learn ... and the scripting modules: http://unity3d.com/learn/tutorials/modules/beginner/scripting - Practically, you can try to fit the enemy destruction code into the player destruction code and see how you get on.
The script is 50% foundation and 50% my work I ve already seen all tutorials about The shooter and I look To the survival shooter Project Nightamare .I haven't The skill To modify that script in My game. One more thing or two.I would add a shield To protect The life of The player and a powerup to recovery life .Could you suggest any tutorials?Your space shooter tutorial doesn't include this solutions. Thanks Simone
As someone who has been following this thread for a couple of days, and having the best intentions towards being constructive, my advice to you, @simone9725 , is that you need to stop and go develop (learn) some basics. You've stated multiple times you don't know the basics of the C# language. You need to know these. You cannot make a game, or any software, only by assembling code snippets--although reuse is a very important thing. At some point, you have to understand how it all works and why it works and how to modify or make new things. I respectfully suggest you stop whatever you're doing and make some good use of the great resources already available to you: http://unity3d.com/learn/tutorials/modules/beginner/scripting I'm quite happy to help with any questions you have or to look for more resources. I'd love to see you make your game and accomplish your goals. I just fear you're on the wrong track and your approach needs to be adjusted. Learning Unity as I, myself, am doing requires you to learn how to pull lots of things together. Programming is only one of these but it is an essential, core part of it. You don't need to be the best programmer in the world, but you need to know the basics and how to read and write the language. You will never get to where you need to be without it, even though Unity can abstract so much of it away--which is great. Best, NJ
I know The bases of csharp But It isn't easily to adapt a code .You have To face many problems The are little functions or string of code that are very important. I Will follow your links To my own culture even so I need help To resolve The problem then I will study.
I'm afraid I have to agree with busy cat. You may know the basics of C#, but: in this thread alone, you have all the tools to solve your problem. You have all the code you need. You just need to put it in the correct order to do the correct thing; and I've told you how to do it. One thing to try to help you think in code is to block out or sketch out your script using "comments" (plain text following two leading //), and once you have the comments in a logical order that feels correct, replace it with real code. This may help untangle your thinking and help you code your script.