Jump to content
  • entries
    44
  • comments
    19
  • views
    2315

100 Days of VR: Day 2 Going through the Unity Space Shooter Tutorial I

Josh Chang

844 views

Hey! Welcome back to the 2nd day of my 100 days of VR!

We last left off going through the Unity Roll a ball tutorial. I’ve learned a lot about how to code in Unity, and how to use some of their existing API’s. Even more importantly, I learned how to navigate and use the Unity editor!

The editor is definitely different compared to Web or Mobile development where you create everything via code, but the editor definitely makes things easier!

Today in day 2, I started to look at the 2nd Unity lesson, the Unity Space Shooter, I’ve learned a lot about Unity from the previous tutorial, maybe even enough for me to start developing my own simple game (with a LOT of help from stackoverflow), but instead I’ve decided to solidify my foundations by going through more 1–2 more tutorials.

I didn’t get nearly as far as I did during Day 1 because of my job, but I did finish the First section:

Game setup, Player and Camera

Setting up the project

Unlike the previous tutorial where we just used simple materials and shapes, this time I had to use actual assets.

The first thing I had to do was download the assets that were provided from the tutorial.

I saw that when you create a new project in Unity. If you click on that tab, you can see the project, you can download it there (or from the Unity asset store)

 
0*7LOvmElmTYJuv8E9.png
 
0*99u0Mtvoj8kXMdRX.png

And then when you create a new project, just make sure to add the asset package the resource:

After setting up the Unity tutorial, I found out that you can adjust the screen resolution of your game, by clicking on the Game and selecting the resolution options below it to set and create the resolution of your game.

 
0*wqXF-qtV-NU_Awdz.png

So far so good, nothing too complex yet!

The Player GameObject

Now that I’ve setup the project, I’ve moved on to create the Player GameObject.

It’s nothing too different from what I’ve learned in the previous tutorial. Except this time, instead of creating a 3D sphere, I’m dragging an existing model asset provided to me.

 
0*ap1C0taSJJfP0F2w.png

Cool ship!

Some interesting things to note on this video

Mesh Collider, Mesh Filter, and Mesh Renderer

Looking at the components of the ship, we see that there are a Mesh Filter and a Mesh Renderer.

From a simple Google search:

Mesh Filter Components take in a Mesh, which is a 3D object that is created with triangles (triangles, because it’s the most efficient shape to process in GUI programming).

The Mesh Filter will then create the shape of what your GameObject will look like based off of the mesh that you provide it.

Afterwards, if you want to see your model, you’ll need a Mesh Renderer this takes in your mesh and applies a material over it. The material being the skin of the ship you see above.

Finally, we have the Mesh Collider Component that we add, that takes in a mesh. This allows us to create a collider that matches the exact shape of our ship.

It’s important to think about the gameplay vs. performance tradeoff that we have to make.

More fine-grain collider => Unity doing more work => Slower performance. However on the other hand, we might not want to create a cube collider over our ship.

 
0*rPDU7VRSIMM0-jMR.png

If we do, we might collide with something on the front of the ship where there aren’t any meshes.

In a more fine grain control game like this space shooter, that would be bad.

Easy way to change your Game Transform values

Also on another un-related note, I found a nifty trick for the editor.

If you want to experiment with values, instead of manually putting the values in, you can actually left click on the properties circled in this picture

 
0*FE2mth3bfuh5IqUp.png

And then from there drag your mouse up and down to change the values. It’s much easier than just putting the values in manually!

Camera and Lighting

In the next video, we played around a bit with the camera and lighting.

Camera

In the camera component you can set the background. In our case, we just set a black background, making everything in the games tab black except for our ship.

Lighting

In the later Unity version, the project already comes with a Directional Light in the hierarchy, but these sources of light, as you expect, lights up your game!

The directional light is just light glowing down from a certain direction.

You can adjust things like the color of the light, how intense the light is, the direction the light comes from and many other things. However in this case, we just played with moving it around a bit and the intensity of the light to get our ship looking however we want it to look.

Adding a background

Creating a background is in a way very similar to adding a material to an existing shape object. For this project, we created a quad, which you can think of as a flat plane that you can’t change the Y scale at all.

We have an existing .tif file that’s provided for us to use to create an object, but our quad object only wants materials. How do we get this to work?

It turns out, if you were to drag the .tif file into the quad, it’ll automatically create the material for you that you can use.

Shaders

Another subject that was briefly talked upon are shaders. From my understanding, shaders are used for rendering effects on the material. It gives us the ability to apply brightness, colors, and other special effects on top of an existing image.

 
0*p39y8U0uFgtcwq_k.png

Luckily we don’t’ have to do learn how to do any of this (yet) and we can just use the existing ones that Unity provides us. Yay for frameworks!

In the video, we applied an Unlit Texture Shader affect to make our background image look brighter.

Moving the Ship

So finally now that we have our environment setup, we finally started to do some coding.

using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
    public float speed;
    public float tilt;
    public Boundary boundary;
    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        rigidbody.velocity = movement * speed;
        rigidbody.position = new Vector3 
        (
            Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
            0.0f, 
            Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
        );
        rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    }
}

Let’s break this code down.

RigidBody

The most important thing right now is that in Unity 5.X, this code actually won’t run. That’s because we no longer get our RigidBody component as we do now.

We have to do something similar to what we did with the roll-a-ball tutorial and save an instance of our RigidBody in the Start() function, like so:

using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
    public float speed;
    public float tilt;
    public Boundary boundary;
    public RigidBody rigidbody;
    void Start()
    {
        Rigidbody = GetComponent<RigidBody>();
    }
    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        rigidbody.velocity = movement * speed;
        rigidbody.position = new Vector3 
        (
            Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
            0.0f, 
            Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
        );
        rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    }
}

We have to use GetComponent<RigidBody> to access our GameObject’s components now.

Boundary

The first thing you might notice is that there’s another class: Boundary. It has [System.Serializable] on top of it.

[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}

What serializable means is that you’re telling Unity if you make the class a public variable it’ll expose the variables in the class for you to use in the inspector.

We’re going to use this boundary to set how far our ship can go in the game. Specifically, prevent our ship from going off screen.

Movement

To move the ship, we applied something very similar to what we did in Day 1 with the ball tutorial.

We see that we still use the same FixedUpdate() function and we grab the players movement input the same way.

The new interesting part is using more of Unity’s library.

Unity comes with its own math library, Mathf. The library provides nifty game calculations for us.

rigidbody.position = new Vector3 
    (
        Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
        0.0f, 
        Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
    );

In this case, we’re using Mathf.Clamp to help set a position boundary so that if our position surpasses the minimum, it’ll stay at the minimum and if it surpasses the maximum, it’ll stay at the maximum.

Creating Shots

So now we have a ship and some movement, we moved on to something completely new, shooting things!

  • We create our bullet the same we create our background.
  • We make a Quad object and then use the .tif file to create a material (or just use the existing one) and attach the material.
  • We applied some shaders on it. We can optimize our performance by using mobile shaders instead of normal ones. Mobile shaders offer less controls, but as a result they’re less computationally intensive.

Finally we have the code that moves our bullet forward:

using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour
{
    public float speed;
    void Start ()
    {
        rigidbody.velocity = transform.forward * speed;
    }
}

The code itself is pretty straight forward, we just set the velocity of our RigidBody to be some value and it’ll always go in that direction.

Shooting shots

Now that we’ve created a prefab of our bullet in the previous lesson, we can start working on shooting.

In the video, what was shown was that we created a new “spawner” GameObject.

A spawner is basically just an empty GameObject that we use its location to create new bullets.

The important thing here is to make the spawner a child of the player GameObject, this way it’ll always stay consistent relative to our ship.

In our script, we added a public variable for both the bullet prefab and the spawn point location.

using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
    public float speed;
    public float tilt;
    public Boundary boundary;
    public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;
    private float nextFire;
    void Update ()
    {
        if (Input.GetButton("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
        }
    }
    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        rigidbody.velocity = movement * speed;
        rigidbody.position = new Vector3 
        (
            Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
            0.0f, 
            Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
        );
        rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    }
}

The part of the code that we’re really interested in is in our new Update() function:

void Update ()
    {
        if (Input.GetButton("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
        }
    }

Inside the code, we use the Input class to detect when the user clicks the fire button (left click) and check for a shooting delay.

The interesting part is how we create our bullet. This is done in Unity with the Instantiate function.

We just need to pass in the GameObject we want to create a copy of, its starting position, and its rotation.

And that’s it!

Conclusion

In day 2, I finished the first part of the Space Shooter tutorial. Once again, a lot of the learning is actually being done outside of code with the editor.

The goods news is that I’m beginning to see similar things with using GameObjects, materials, and prefabs to name some examples.

Code-wise, everything seems pretty straightforward. Now I might be biased, because I already know how to code, but I have a good feeling that we’ll see many of these API’s again.

It’s my hope that I’ll see patterns and that I use to make my own game.

My goal is to know enough what’s available in Unity so I can branch out and start writing my own code!

Side note: wow these writes up are long! I’m actually wondering what takes longer, going through the video’s and learning and understanding what’s happening or writing these explanations!

Visit the original Day 2

Go to the 100 Days of Unity VR Development Main Page



4 Comments


Recommended Comments

Read your posts and have to say that you are truly ambitious. A VR game in a 100 days is going to be difficult. 

Like how you made these blogs. Keep up the good work.

Share this comment


Link to comment

Haha thanks. I say "100 days", but it's definitely not going to be 100 days in a row, I definitely don't have the bandwidth to do my day job, learn Unity at home, and then make a post about it everyday!

As the saying goes, "Shoot for the moon. Even if you miss, you'll land among the stars."

Share this comment


Link to comment

Thanks for the heads up. I didn't know who it belonged to, but I had a feeling I shouldn't use it, but I did...

I was testing placeholders while my own logo was being created and was hoping to have gotten it by now... anyways removed.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Advertisement
  • Blog Entries

  • Similar Content

    • By Aileen Chen
      2019 Asia VR&AR Fair&Summit (VR&AR Fair 2019)
      Date: May 9-11, 2019
      Venue: China Import&Export Fair Complex, Guangzhou, China
      Address: No. 382, Yuejiang Zhong Road, Guangzhou, China
      Website: http://www.vrarfair.com/index.php?lang=en
      Hosted by: Guangdong Grandeur International Exhibition Group
      Co-organized by: Guangzhou Virtual Reality Industry Association, Guangdong VR Industry Alliance, Shenzhen Virtual Reality Industry Federation, Shenzhen Virtual Reality Industry Association
       
      Overview
      As a thematic exhibition of China Guangzhou International Leisure & Recreation Expo (GILE),VR&AR Fair has been successfully held for two consecutive years (Twice a year, Guangzhou and Wuhan), becoming the One and Only Professional Demonstration and Trade Platform of the VR&AR Industry in Guangzhou. Over the years, we has gathered numbers of famous companies at home and abroad to display their latest products and technologies at VR&AR Fair, including JD.COM, 3Glassess, DP VR, Leke VR, PiXYZ Software (France), ICAROS(Germany), Ai6tech (Taiwan), VRway, Hirain, Royole, Super Captain, TPCAST VR, Shenlinqijing, Foldspace, NineD, TPcast, etc. 
       
      VR&AR Fair 2019 is estimated to host over 250 exhibitors and co-located with 2019 Asia Amusement&Attractions Expo (AAA 2019) on a show floor of 10,0000 sq.m, which are going to cover sectors like VR helmet accessories, VR all-in-one machine, interactive multimedia products, immersive games and devices, immersive digital cinemas, virtual walk-through products, auto stereoscopic (glasses-free), 3D-9D cinema devices, multi-touch devices, AR equipment, AR game, environment modeling technology, realistic sensor (real-time) rendering technology, amusement equipment, Theme Park Facilities,etc. 
       
      Concurrent event:
      2019 Asia Science Museum & Exhibition Hall Facilities Expo
      2019 Asia Multimedia Technology & Interactive Projection Expo
      The 10th Asia Theater & Filming Equipment Fair 2019
      2019 Asia Amusement & Attraction Expo
       
      Why VR&AR Fair 2019?
      1. Win face-to-face business opportunities
      2. Seek professional buyers
      3. Come into contact with business decision makers
      4. Maintain existing clients and find new clients
      5. Effectively improve brand image & awareness
      6. Accurately position your company and brand
      7. Grab market shares
      8. Get access to the latest industrial development
      9. Vigorously expand product and service scope
      10. Establish extensive agent and investment attraction network
       
       
      Discover Your Opportunity at VR&AR Fair 2019!
      Aileen Chen
      Director of Guangdong Grandeur International Exhibition Group
      Tel: 82-20-29806525  
      Fax: 86-20-82579220 
      Mobile/Wechant: 15089702986
      E-mail: grand.fi@grahw.com 
       
       
       
    • By Liam Weston
      Greetings fellow gaming enthusiasts!
      I am the Product Manager for a VR game called Funny Farm VR.
      https://www.funnyfarmvr.com/
      We have already have a proof of concept build of the game that is published on iOS, Android, Oculus Rift, Oculus Go, GearVR, Pico and Niburu platforms. Not a bad start eh? 
      It's a 3D VR game built on the Unity platform. We are now looking to further develop the game concept and build on what we have to achieve the following:
      Introduce a game economy (power ups / rewards / currency) Add more levels We need 1 or 2 developers with experience in developing on the Unity 3D platform as well as a 3D artist and animator.
      This is a great opportunity to work on a fun game and inject your own ideas / personality into it.
      If you're interested in getting involved drop me a message and I'll get in touch.
      Looking forward to putting a team together!
      Liam
       
    • By Questengine
      I made a VR game for Oculus Rift.  It feature complete, but of course some assets are incomplete and balance needs work.  If you have an Oculus Rift and you're looking for something to do with it, you could do worse!
      It's a puzzle/resource management game disguised as a space shooter.  You're in a space ship cockpit and the guns are shooting the aliens, but that's all automatic and passive.  You just maintain ammo supplies, manage heat, recover loot and keep the ship shielded.  There's lots of switches and do-dads scattered all around the cockpit to keep you busy. It's very VR friendly: you have a mostly stationary cockpit, lots of reasons to turn and look and use you motion controllers, and games can be very short.
      This beta is very configurable too.  There's a plain old text file where you can change lots of the static values the game uses.  I'd actually like help balancing everything to be honest as it's a bit complicated.  
      If you have an Oculus Rift I hope you'll take a look.  
      http://www.questengine.com/vercs


    • By OfficePassenger
      Dear frends, I would like to try myself as a composer for games, I make unusual music with an unusual mood and atmosphere, I like to work on sounds and details in music, below I attach examples of my works, [deleted by moderator]
      1.mp3
      2.mp3
      3.mp3
      4.mp3
      5.mp3
      6.mp3
      7.mp3
      8.mp3
    • By sdef Lab
      Hi,
      We have designed VR project with a large area database and when we are making our aircraft to move around the scene , we are facing a spatial jittering when trying to move far from origin
      any solution???.
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!