In a recent Game Jam with my buddies Tony and Larry, we came up with the idea of doing a tower defense game in VR. In this short blog post I’ll go over how to easily setup a sniper scope in Unity, so that your players can actually peer through a tactical scope in VR in order to snipe at their enemies from afar.
In my experience, there’s just something about having a second screen experience that enhances the immersion and sense of presence. A second screen experience in VR is a mechanism that provides a different perspective from the player avatar’s viewpoint. Examples include watching a TV that projects the image of a security camera, watching someone in the real world do something live on a TV in VR, or in our case today use a sniper scope to zoom in on an enemy and take them out.
I think there are a few reasons why this type of experience may be compelling:
- Anything that has several layers of interactability in VR is interesting. A sniper rifle + scope is a much more interesting item then the gun itself. Another example is a camera with a selfie stick where one can take pictures of our virtual representations in VR ala Facebook Spaces
- Having a screen within a screen is a recursive a pattern which “tricks” the brain into forgetting that it actually has a VR device on it. By adding a second screen, we are creating a more believable reality by adding reference points that are anchored to our reality
These are just hypotheses of course. However, one thing is certain, the subjective experience of peering through a second screen is just flat out cool. As it turns out, settingup a basic experience in VR without the bells and whistles is pretty simple. Here’s an example from a weekend gamejam I did with Larry Charles and Tony Nguyen
We were able to create this sniper rifle tower defense game over the course of the weekend and the sniper scope itself was easy to setup, as you’ll see below.
To setup a sniper scope of your own, follow this cookbook:
- You can get an awesome ACOG scope and set of modern weapons from the asset store for free here: https://www.assetstore.unity3d.com/en/#!/content/14233
- You’ll want to resize the scope of the model you find here to be at least double the size. Although this makes the sniper rifle less realistic, from a gameplay perspective, it makes it much easier to see what’s on the scope.
- In my example, I rotated the scope so that the lens with the larger surface area was facing me.
- Create a render texture (A render texture is a texture which takes input from a camera and display’s it on the surface of the texture)
- On Windows it’s Toolbar/Assets/Create/Render Texture
- For the sniper rifle, because the player will be getting their face right to the edge of the scope, I set the render texture to 1024×1024
- Create a material – A wrapper for shader properties and other parameters to tell unity how to render a particular object. In this case, it will be the thing that links your render texture with the actual rendering of the scope view on a plane you’ll create in step 5
- Toolbar/Assets/Create/New Material
- Drag the render texture you created in step 1 onto the Albedo map of the material.
- Create a second camera, this will serve as camera view of the scope
- Drag the render texture onto the “Target Texture” field of your scope camera
- Set the field of view for the camera to your desired zoom level. The smaller the field of view, the higher the zoom level.
- Position the camera to be at the end of the barrel of the gun. Although this is another sacrifice for realism, you’ll obviously want the bullet firing where the gun is aiming.
- Create a Plane Game Object — The plane is the object that you’ll drop your texture onto. It’s what renders the camera view you just created
- Toolbar/GameObject/3D Object/Plane
- Under the Materials section of the Mesh Renderer Component on the plane you created, drag in the material you created in step 3.
- At this point, you should see the entire plane you created render the camera view that you assigned to the render texture.
- Resize, reposition, and rotate the plane so that it matches the size of your scope. Make sure you drag the plane in the hierarchy to be a child of the sniper scope so it moves with the rifle.
At this point you should be able to press play and see your sniper scope on your second lens when you zoom in. Further enhancements to this project could be to create a shader or circular “plane” which renders the sniper rifle so you don’t see the square edges, as well as some distortion to make the scope seem like an actual lens, but are outside the scope of this tutorial.
Feel free to let me know if you have any questions about implementation on twitter @Andy_Tsen or dropping me a line via: email@example.com