Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Shooting ducks - Help needed

Discussion in '2D' started by zardtomcat, Feb 14, 2016.

  1. zardtomcat

    zardtomcat

    Joined:
    Jan 10, 2015
    Posts:
    17
    Hi,
    I am developing a game for our primary school kids. I have two rows of ducks and the kids need to shoot the ducks by moving a crosshair to aim at the duck and then shoot it. There is no blood and it shoots a little flower.

    I am looking for any tutorial which could help me to do create the shooting game and move the crosshair around but haven't found any. being fairly new to Unity, does anybody know of any tutorials which might help?

    Thanks
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    how you will move crosshair ? I mean, for what platform will be game, and what input device shopuld be used ?
     
  3. zardtomcat

    zardtomcat

    Joined:
    Jan 10, 2015
    Posts:
    17
    Not sure how this helps!!
    I am looking for a tutorial on how to do it, so if you know of one, appreciate a link to it. We use cursor keys on a laptop to move it.
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    sorry, i dont know tutorial for duck shooting. But it's not the game, that is hard to do. I can try to explain :)

    start new unity 2d project. Drag&drop your duck sprite in Asset folder. Put this duck in scene. Now you should have new gameObject in Hierarchy. Choose it and in inspector add to it: collider2D, rigidbody2d (set gravity scale to 0, interpolate to interpolate). Right click in Asset window create new c# script, name it duckMover. Double click on it, delete all lines and insert:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class duckMover : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     Rigidbody2D rb;
    8.  
    9.     void Start () {
    10.         rb = GetComponent <Rigidbody2D> ();
    11.         rb.velocity = new Vector3 (speed, 0, 0);
    12.     }
    13. }
    14.  
    Add this script on duck gameObject. Set speed to something (1, -1). Now if you start scene, the duck should move left or right, depended on speed.

    Make new c# script. Name it: offScreenDestroy
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class offScreenDestroy : MonoBehaviour {
    5.  
    6.     void OnBecameInvisible () {
    7.         Destroy (gameObject);
    8.     }
    9. }
    10.  
    add this script on duck too. This script will destroy duck, if it will be not visible (just for performance save).

    Then drag&drop your duck gameobject from hirarchy, to asset folder, you should have now one prefab from duck. Ok, ducks should be ready :)
     
    Last edited: Feb 14, 2016
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    now make duck spawner. Add empty gameobject in hirarchy (right click in hirarchy window and pick create empty). Name it duckSpawner. Place this spawner somewhere in scene, where ducks should be born.
    crate new c# script, name it: duckSpawner
    open it, delete all and insert
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class duckSpawner : MonoBehaviour {
    5.     public GameObject duck;
    6.     public float delay;
    7.  
    8.     void Start () {
    9.         InvokeRepeating ("duckSpawn", 0, delay);
    10.     }
    11.  
    12.     void duckSpawn () {
    13.         Instantiate (duck, transform.position, transform.rotation);
    14.     }
    15. }
    16.  
    add this script to duckSpawner gameobject. Set delay (in inspector of spawner) to something like 3. Drag&drop your duck prefab (it should be in asset) in duck field (in inspector from duckSpawner).

    Start scene, the ducks shold be born and they will move somewhere. Its tha half of the game :)

    ps. take care of z-axe. Spawner should be on 0, camera on -10 (not exactly, just camera befor spawner)
     
  6. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    add new empty gameobject (set z-position to -5. You should have z: camera -10, crossHair -5, duckSpawner 0)). Name it crossHair. Add collider2d to it (set trigger on in inspector), rigidbody2d (set gravity to 0, interpolate to interpolate). Create new c# script, name it: crossMover
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class crossMover : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     Rigidbody2D rb;
    8.  
    9.     void Start () {
    10.         rb = GetComponent<Rigidbody2D> ();
    11.     }
    12.  
    13.     void FixedUpdate () {
    14.         float xMove = Input.GetAxis ("Horizontal");
    15.         float yMove = Input.GetAxis ("Vertical");
    16.         Vector3 move = new Vector3 (xMove, yMove, 0)*speed;
    17.         rb.velocity = move;
    18.     }
    19. }
    20.  
    Add script to crossHair set speed like 5. Start scene and try cursor keys.
     
  7. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    add new c# script. name it: duckKiller
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class duckKiller : MonoBehaviour {
    5.  
    6.     void Update() {
    7.         if (Input.GetKeyDown (KeyCode.Space)) {
    8.             RaycastHit2D hit = Physics2D.Raycast (transform.position, Vector3.forward);
    9.             if (hit.collider != null)
    10.                 Destroy (hit.collider.gameObject);
    11.         }
    12.     }
    13. }
    14.  
    add this script on crossHair.

    Delete collider2d from crossHair. It is destroyg it itself (i am not using raycasts much, can not say now how to do better)

    Start and with space you should be able destroy ducks.
     
  8. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    at last polish the new blockbuster tripleA game :oops:
     
  9. zardtomcat

    zardtomcat

    Joined:
    Jan 10, 2015
    Posts:
    17
    Hi vakabaka
    Thanks, this is very helpful.:) and very much appreciate the explanation. I will put this together and hopefully it works out. I let you know how I get on.
     
  10. zardtomcat

    zardtomcat

    Joined:
    Jan 10, 2015
    Posts:
    17
    Hi vakabak
    I followed the instructions and they worked well. I made a very rough demo and kids liked the idea but there are lots of things missing as you expect.
    I changed the duckSpawner so now ducks come from different locations on the screen. I need to do a couple of things though and wonder what is best way to do it.
    1) I need to put the scores on the top of the screen. Using a fun font would be good, but I use whatever unity offers. What is the best way of doing this?
    2) When they score I need to show "Well done" message on the screen. I can make a sprite, but dont know how to make a sprite visible and then hide it, if this is the way i have to do it.
    3) I need to show a certificate which has their score. Again a sprite is a good idea but not sure how to add their name to an existing sprite and dont know how to show the sprite and hide it.

    I havent used GUIs in unity so I was thinking about IMGUI, Any ideas about whether this help to do the above?

    Thanks
     
  11. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I dont know what the best way for all that is :)

    "well done". You can place sprite in the scene, and then enable and disable them (gameobject.setactive (true); or false). I think its more easy just instantiate them and then destroy.
    add sprite in scene. Make script for destroy (I dont write now script name, just make your c# script and delete lines:
    // Use this for initialization
    void Start () {
    }
    // Update is called once per frame
    void Update () {
    }
    and then insert between:
    Code (CSharp):
    1. public float destroyTime;
    2. void Start () {
    3. Destroy (gameObject, destroyTime);
    4. }
    add this script to well done sprite. Make prefab of this gameobject (draganddrop it in asset folder). Delete it from scene.
    now you can instatiate "well done", and it will delete it itself from scene. You can make something like (i have used duckKiller script):
    Code (CSharp):
    1.  
    2.  
    3.     public GameObject wellDone;
    4.  
    5.     void Update() {
    6.         if (Input.GetKeyDown (KeyCode.Space)) {
    7.             RaycastHit2D hit = Physics2D.Raycast (transform.position, Vector3.forward);
    8.             if (hit.collider != null){
    9.                 Instantiate (wellDone, hit.collider.gameObject.transform.position, transform.rotation);
    10.                 Destroy (hit.collider.gameObject);
    11.             }
    12.         }
    13.     }
    14.  
    this script should place wellDone sprite on hitted ducks place.
    you can change line to:
    Instantiate (wellDone, transform.position, transform.rotation);
    then it will instantiate on duckkiller place (you can move it in scene to right place (beware z-axis).
     
  12. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    for score you can use unity ui.
    click on top: GameObject - > UI > Text.
    You should have canvas + text in hirarchy. Now it is little tricky. Dont place the text object in main camera view. Double click on canvas, you should see the white squre for ui view. Place the text object as you want (dont forget: for ui placment doble click on canvas, for gameobject placement double click on main camera). Choose text in canvas (you can rename it), adjust it in inspectror (maybe: alignment in the middle, best fit - on, color). Start scene check the text. If you like it, then ok.
    If your script should work with ui, then dont forget add: Using UnityEditor.UI; on top of script. I am lazy, so i will use duckKiller again. It will be that:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class duckKiller : MonoBehaviour {
    6. //prefab welldone
    7.     public GameObject wellDone;
    8. //drag&drop here text gameObject
    9.     public GameObject counter;
    10. //this variable for score
    11.     public int score;
    12.     Text counterText;
    13.  
    14.     void Start () {
    15. //text componente
    16.         counterText = counter.GetComponent <Text> ();
    17.     }
    18.  
    19.     void Update() {
    20.         if (Input.GetKeyDown (KeyCode.Space)) {
    21.             RaycastHit2D hit = Physics2D.Raycast (transform.position, Vector3.forward);
    22.             if (hit.collider != null){
    23.                 Instantiate (wellDone, hit.collider.gameObject.transform.position, transform.rotation);
    24. //short version to score = score + 1;
    25.                 score ++;
    26. //set to score, the variable shold be string, so change int to string
    27.                 counterText.text = score.ToString();
    28.                 Destroy (hit.collider.gameObject);
    29.             }
    30.         }
    31.     }
    32. }
    you can try to change unity font: http://docs.unity3d.com/Manual/class-Font.html
     
    Last edited: Feb 21, 2016