Although using GUI elements, such as a cross-hair, is a valid way to allow players to aim, replacing (or combining) it with a projected laser dot might be a more interesting approach. In this recipe, we will use the Projector and Line components to implement this concept.
To help us with this recipe, it's been provided with a Unity package containing a sample scene featuring a character holding a laser pointer, and also a texture map named LineTexture
. All files are inside the 1362_06_03
folder. Also, we'll make use of the Effects assets package provided by Unity (which you should have installed when installing Unity).
To create a laser dot aim with a Projector, follow these steps:
BasicScene.unitypackage
to a new project. Then, open the scene named BasicScene. This is a basic scene, featuring a player character whose aim is controlled via mouse.ProjectorLight
shader (inside the Assets | Standard Assets | Effects | Projectors | Shaders folder). Duplicate the file and name the new copy as ProjectorLaser
.ProjectorLaser
. From the first line of the code, change Shader "Projector/Light"
to Shader "Projector/Laser"
. Then, locate the line of code – Blend DstColor One
and change it to Blend One One
. Save and close the file.The reason for editing the shader for the laser was to make it stronger by changing its blend type to Additive. Shader programming is a complex subject, which is beyond the scope of this book. However, if you want to learn more about it, check out Unity's documentation on the subject, which is available at http://docs.unity3d.com/Manual/SL-Reference.html, and also the book called Unity Shaders and Effects Cookbook, published by Packt.
LaserMaterial
. Then, select it from the Project view and, from the Inspector view, change its Shader to Projector/Laser.255
, 0
, 0
). Then, from the texture slots, select the Light texture as Cookie and the Falloff texture as Falloff.0.1
. Finally, select LaserMaterial from the Material slot.255
;0
;0
).LineTexture
image file. Then, set it as the Particle Texture for the Line_Mat, as shown:LaserAim
. Then, open it in your editor.using UnityEngine; using System.Collections; public class LaserAim : MonoBehaviour { public float lineWidth = 0.2f; public Color regularColor = new Color (0.15f, 0, 0, 1); public Color firingColor = new Color (0.31f, 0, 0, 1); public Material lineMat; private Vector3 lineEnd; private Projector proj; private LineRenderer line; void Start () { line = gameObject.AddComponent<LineRenderer>(); line.material = lineMat; line.material.SetColor("_TintColor", regularColor); line.SetVertexCount(2); line.SetWidth(lineWidth, lineWidth); proj = GetComponent<Projector> (); } void Update () { RaycastHit hit; Vector3 fwd = transform.TransformDirection(Vector3.forward); if (Physics.Raycast (transform.position, fwd, out hit)) { lineEnd = hit.point; float margin = 0.5f; proj.farClipPlane = hit.distance + margin; } else { lineEnd = transform.position + fwd * 10f; } line.SetPosition(0, transform.position); line.SetPosition(1, lineEnd); if(Input.GetButton("Fire1")){ float lerpSpeed = Mathf.Sin (Time.time * 10f); lerpSpeed = Mathf.Abs(lerpSpeed); Color lerpColor = Color.Lerp(regularColor, firingColor, lerpSpeed); line.material.SetColor("_TintColor", lerpColor); } if(Input.GetButtonUp("Fire1")){ line.material.SetColor("_TintColor", regularColor); } } }
Line_Mat
material, as shown:In this recipe, the width of the laser beam and its aim dot have been exaggerated. Should you need a more realistic thickness for your beam, change the Line Width field of the Laser Aim component to 0.05
, and the Orthographic Size of the Projector component to 0.025
. Also, remember to make the beam more opaque by setting the Regular Color of the Laser Aim component brighter.
The laser aim effect was achieved by combining two different effects: a Projector and Line Renderer.
A Projector, which can be used to simulate light, shadows, and more, is a component that projects a material (and its texture) onto other game objects. By attaching a projector to the Laser Pointer object, we have ensured that it will face the right direction at all times. To get the right, vibrant look, we have edited the projector material's Shader, making it brighter. Also, we have scripted a way to prevent projections from going through objects, by setting its Far Clip Plane on approximately the same level of the first object that is receiving the projection. The line of code that is responsible for this action is—proj.farClipPlane = hit.distance + margin;
.
Regarding the Line Renderer, we have opted to create it dynamically, via code, instead of manually adding the component to the game object. The code is also responsible for setting up its appearance, updating the line vertices position, and changing its color whenever the fire button is pressed, giving it a glowing/pulsing look.
For more details on how the script works, don't forget to check out the commented code, available within the 1362_06_03
| End
folder.
18.118.186.202