Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / Javascript

Unity3D Brick Shooter

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
19 Jan 2015CPOL1 min read 8K   2  
Unity3D Brick shooter

Followed this official tutorial, my own version on GitHub, and you can see it in action here (WSAD to move, mouse click to shoot).

Steps on how to create this:

  1. Create a new 3D project
  2. Save the scene as Main
  3. Create -> 3D object -> Plane
  4. Create -> 3D object -> Cube
    1. Add Component -> Physics -> Box collider
    2. Add Component -> Physics -> Rigidbody
    3. Drag and drop this object in the Assets/Prefabs folder
    4. Make duplicates (use snapping to move by using the ctrl key)
    5. Make a row of them (about 8) and then make a new empty object and drag them all into this object
    6. Now duplicate this object and position above (again by using snapping with ctrl key)
  5. Create -> 3D object -> Sphere
    1. Add Component -> Physics -> Sphere collider
    2. Add Component -> Physics -> Rigidbody
    3. Make it into a prefab
    4. Delete it from Hierarchy
  6. On the camera object => Add Component -> Script:
    1. JavaScript
      #pragma strict
      
      public var projectile : Rigidbody;
      public var shotPos : Transform;
      public var shotForce : float = 1000f;
      public var moveSpeed : float = 10f;
      
      function Update ()
      {
          var h : float = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
          var v : float = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
          
          transform.Translate(new Vector3(h, v, 0f));
          
          if(Input.GetButtonUp("Fire1"))
          {
              var shot : Rigidbody = Instantiate(projectile, shotPos.position, shotPos.rotation);
              shot.AddForce(shotPos.forward * shotForce);
          }
      }
    2. Drag the Sphere prefab to the projectile.
  7. Create empty game object and rename to shotPos.
    1. Drag and drop it on the camera
    2. Reset transform origin
    3. Position y=-0.5, z=1
    4. Rotation x=-15
  8. Drag Camera to shot pos variable in the script.
  9. If you want to clean up the bullets after 2 seconds, add script to Sphere prefab:
    JavaScript
    function Start () {
    	Destroy(gameObject, 2f);
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Croatia Croatia
I’m an engineer at heart and a jack of all trades kind of guy.

For those who care about titles, I hold a masters degree in computing from FER (and a black belt in karate, but that’s another story…).

During the last years, worked in a betting software industry where I made use of my knowledge in areas ranging from full-stack (web & desktop) development to game development through Linux and database administration and use of various languages (C#, PHP, JavaScript to name just a few).

Currently, I’m a senior software engineer at TelTech, where we make innovative communications apps, and I <3 it.

Lately, I’m very passionate about Ionic framework and am currently in the top 3 answerers on StackOverflow in Ionic framework. I wrote a book about Ionic framework which you can get for free on Leanpub: Ionic framework – step by step from idea through prototyping to the app stores.

Other technical writing:

+ wrote a book Getting MEAN with MEMEs
was a technical reviewer for a book Deploying Node published by Packt
was a technical reviewer for a book Getting started with Ionic published by Packt
After writing 300 posts, this is why I think you should start blogging too

Come and see what I write about on my blog.

Comments and Discussions

 
-- There are no messages in this forum --