Search Unity

Life shooter

Discussion in '2D' started by simone9725, Jan 26, 2015.

  1. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    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):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. [System.Serializable]
    5. public class Boundary
    6. {
    7.     public float xMin, xMax, yMin, yMax; //Screen Boundary dimentions
    8. }
    9. public class Player_Script : MonoBehaviour
    10. {
    11.     //Public Var
    12.     public float speed;             //Player Ship Speed
    13.     public Boundary boundary;         //make an Object from Class Boundary
    14.     public GameObject shot;            //Fire Prefab
    15.     public Transform shotSpawn;        //Where the Fire Spawn
    16.     public float fireRate = 0.5F;    //Fire Rate between Shots
    17.     public GameObject Explosion;    //Explosion Prefab
    18.     public GameObject restartDialog; //eND gAME
    19.            
    20.     //Private Var
    21.     private float nextFire = 0.0F;    //First fire & Next fire Time
    22.     private bool  dead;
    23.     void Start () {
    24.     restartDialog.SetActive(false);
    25.     }
    26.     // Update is called once per frame
    27.     void Update ()
    28.         {
    29.         //Excute When the Current Time is bigger than the nextFire time
    30.         if (Time.time > nextFire)
    31.         {
    32.             nextFire = Time.time + fireRate;                                 //Increment nextFire time with the current system time + fireRate
    33.             Instantiate (shot , shotSpawn.position ,shotSpawn.rotation);     //Instantiate fire shot
    34.             audio.Play ();                                                     //Play Fire sound
    35.         }
    36.     }
    37.     // FixedUpdate is called one per specific time
    38.     void FixedUpdate ()
    39.     {
    40.         float moveHorizontal = Input.GetAxis ("Horizontal");                 //Get if Any Horizontal Keys pressed
    41.         float moveVertical = Input.GetAxis ("Vertical");                    //Get if Any Vertical Keys pressed
    42.         Vector2 movement = new Vector2 (moveHorizontal, moveVertical);         //Put them in a Vector2 Variable (x,y)
    43.         rigidbody2D.velocity = movement * speed;                             //Add Velocity to the player ship rigidbody
    44.         //Lock the position in the screen by putting a boundaries
    45.         rigidbody2D.position = new Vector2
    46.             (
    47.                 Mathf.Clamp (rigidbody2D.position.x, boundary.xMin, boundary.xMax),  //X
    48.                 Mathf.Clamp (rigidbody2D.position.y, boundary.yMin, boundary.yMax)    //Y
    49.             );
    50.     }
    51.     //Called when the Trigger entered
    52.     void OnTriggerEnter2D(Collider2D other)
    53.     {
    54.         //Excute if the object tag was equal to one of these
    55.         if(other.tag == "Enemy" || other.tag == "Asteroid" || other.tag == "EnemyShot")
    56.         {
    57.             Instantiate (Explosion, transform.position , transform.rotation);
    58.                                                     //Trigger That its a GameOver
    59.             Destroy(gameObject);
    60.             restartDialog.SetActive(true);
    61.         }
    62.     }
    63.     public void RestartGame()
    64.     {
    65.         Application.LoadLevel (Application.loadedLevelName);
    66.     }
    67.  
    68.     public void ExitToMenu()
    69.     {
    70.         Application.LoadLevel ("Menu");
    71.     }
    72.     //Destroy Player Ship Object
    73.         }
    74.    
    75.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyRed_Script : MonoBehaviour
    5. {
    6.  
    7.     //Public Var
    8.     public float speed;                        //Enemy Ship Speed
    9.     public int health;                        //Enemy Ship Health
    10.     public GameObject LaserGreenHit;        //LaserGreenHit Prefab
    11.     public GameObject Explosion;            //Explosion Prefab
    12.     public int scoreValue;                    //How much the Enemy Ship give score after explosion
    13.     public GameObject shot;                    //Fire Prefab
    14.     public Transform shotSpawn;                //Where the Fire Spawn
    15.     public float fireRate = 0.5F;            //Fire Rate between Shots
    16.  
    17.     //Private Var
    18.     private float nextFire = 0.0F;            //First fire & Next fire Time
    19.  
    20.     // Use this for initialization
    21.     void Start ()
    22.     {
    23.         rigidbody2D.velocity = -1 * transform.up * speed;    //Enemy Ship Movement
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update ()
    28.     {
    29.         //Excute When the Current Time is bigger than the nextFire time
    30.         if (Time.time > nextFire)
    31.         {
    32.             nextFire = Time.time + fireRate;                                     //Increment nextFire time with the current system time + fireRate
    33.             Instantiate (shot , shotSpawn.position ,shotSpawn.rotation);         //Instantiate fire shot
    34.             audio.Play ();                                                        //Play Fire sound
    35.         }
    36.     }
    37.  
    38.     //Called when the Trigger entered
    39.     void OnTriggerEnter2D(Collider2D other)
    40.     {
    41.         //Excute if the object tag was equal to one of these
    42.         if(other.tag == "PlayerLaser")
    43.         {
    44.             Instantiate (LaserGreenHit, transform.position , transform.rotation);        //Instantiate LaserGreenHit
    45.             ScoreManager.score += scoreValue = 300;  
    46.             Destroy(other.gameObject);                                                    //Destroy the Other (PlayerLaser)
    47.            
    48.             //Check the Health if greater than 0
    49.             if(health > 0)
    50.                 health--;                                                                 //Decrement Health by 1
    51.            
    52.             //Check the Health if less or equal 0
    53.             if(health <= 0)
    54.             {
    55.                 Instantiate (Explosion, transform.position , transform.rotation);         //Instantiate Explosion
    56.                                                     //Increment score by ScoreValue
    57.                 Destroy(gameObject);                                                    //Destroy The Object (Enemy Ship)
    58.             }
    59.         }
    60.        
    61.     }
    62. }
    63.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class EnemyShot_Script : MonoBehaviour
    4. {
    5.     //Public Var
    6.     public float speed; //EnemyRed Shot Speed
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.         rigidbody2D.velocity = -1 * transform.up * speed; //Give Velocity to the Enemy ship shot
    11.     }
    12. }
    13.  
     
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    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.
     
  3. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    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
     
    Last edited: Jan 26, 2015
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You have this in your code:
    Code (csharp):
    1. //Called when the Trigger entered
    2.    void OnTriggerEnter2D(Collider2D other)
    3.    {
    4.        //Excute if the object tag was equal to one of these
    5.        if(other.tag == "PlayerLaser")
    6.        {
    7.            Instantiate (LaserGreenHit, transform.position , transform.rotation);        //Instantiate LaserGreenHit
    8.             ScoreManager.score += scoreValue = 300;
    9.            Destroy(other.gameObject);                                                    //Destroy the Other (PlayerLaser)
    10.      
    11.            //Check the Health if greater than 0
    12.            if(health > 0)
    13.                 health--;                                                                //Decrement Health by 1
    14.      
    15.            //Check the Health if less or equal 0
    16.            if(health <= 0)
    17.            {
    18.                Instantiate (Explosion, transform.position , transform.rotation);        //Instantiate Explosion
    19.                                                    //Increment score by ScoreValue
    20.                Destroy(gameObject);                                                    //Destroy The Object (Enemy Ship)
    21.            }
    22.        }
    23.     }
    Wouldn't it be similar to this?
     
  5. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    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?
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'm confused, then.

    What you need to do, is find the place where your code destroys the player ship and change that to:
    1. Create a health value for the player ship.
    2. Reduce the player's health each time it is hit by the amount of damage from the object hitting it.
    3. 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):
    1. 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.
     
  7. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Destroy : MonoBehaviour {
    5.     public GameObject Explosion;    //Explosion Prefab
    6.     public GameObject restartDialog; //eND gAME
    7.     // Use this for initialization
    8.     void Start () {
    9.         restartDialog.SetActive(false);
    10.     }
    11.    
    12.     //Called when the Trigger entered
    13.     void OnTriggerEnter2D(Collider2D other)
    14.     {
    15.         //Excute if the object tag was equal to one of these
    16.         if(other.tag == "Enemy" || other.tag == "Asteroid" || other.tag == "EnemyShot")
    17.         {
    18.            
    19.            
    20.             {
    21.                 Instantiate (Explosion, transform.position , transform.rotation);        
    22.                 //Trigger That its a GameOver
    23.                 Destroy(gameObject);
    24.                 restartDialog.SetActive(true);
    25.                
    26.             }
    27.         }
    28.     }
    29.     public void RestartMenu()
    30.     {
    31.         Application.LoadLevel (Application.loadedLevelName);
    32.     }
    33.    
    34.     public void ExitToMenu()
    35.     {
    36.         Application.LoadLevel ("Menu");
    37.     }
    38.     //Destroy Player Ship Object
    39.    
    40. }
    41.  
    This is the code that use to destroy the player. I haven't any code to reduce and invoke the function damage
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Right!

    Now, instead of line 23:
    Code (csharp):
    1. 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.
     
  9. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    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
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    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.
     
    theANMATOR2b likes this.
  11. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    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
     
  12. BusyCat

    BusyCat

    Joined:
    Jan 9, 2015
    Posts:
    37
    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
     
    theANMATOR2b likes this.
  13. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    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.
     
  14. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    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.
     
    BusyCat likes this.