Today I do a FPS game, because I do the third person shooting, so need a gun infrared aiming effect.
At first I hung a very thin, long spotlight on the gun, looking at the distant object as though it was a little infrared, but the light that came into the light as a flashlight was not the effect I wanted.
Later I use the particle effect, although the distance near is a ray, but the effect is very rough, not the kind of thin and penetrating feeling, but like a laser, and feel constantly generated particles, the performance of the consumption will be very large.
Finally on the Internet to see someone with Unity3.5 self-contained example angrybots inside has infrared aiming effect, but the code is a bit problematic, changed a bit, the feeling effect can also.
The effect of infrared aiming is achieved:
1. Create an empty node called the laser, add the line renderer component, which relates to a material, Materials use angrybots inside the lasermaterial.mat, texture textures are lasertexture.psd and simplenoise.psd. Shader uses the Laserscope.shader inside the angrybots. This is Ray.
2. Create a plane called Laserdot as a sub-node of the laser node, scale is set to 0.02, the material uses the Laserdot.mat inside the angrybots, the material texture map is laserdot.psd and SIMPLENOISE.PSD. Shader uses the Laserscope.shader inside the angrybots. This is a red point when the surface of the object is shot.
3. Create a Script component Preframeraycast is mounted below the laser to emit a ray and return the collision information of the Ray
usingUnityengine;usingSystem.Collections;//Reprint Please indicate the source Public classpreframeraycast:monobehaviour{PrivateRaycasthit Hitinfo; PrivateTransform tr; //Use this for initialization voidStart () {}voidAwake () {TR= This. Transform; } //Update is called once per frame voidUpdate () {Hitinfo=NewRaycasthit (); Physics.raycast (Tr.position, Tr.forward, outhitinfo); Debug.drawray (Tr.position, Tr.forward, color.red); } //returns the collision information of the Ray PublicRaycasthit Gethitinfo () {if(Hitinfo.equals (NULL) {debug.logwarning ("hitinfo is null"); } returnHitinfo; }}
4. Create a Script component Pointerctrl is mounted below the laser to draw infrared rays
Open PointerCtrl.cs
usingUnityengine;usingSystem.Collections;//Reprint Please indicate the source Public classpointerctrl:monobehaviour{ Public floatScrollspeed =0.5f; Public floatPulsespeed =1.5f; Public floatNoisesize =1.0f; Public floatMaxWidth =0.5f; Public floatMinWidth =0.5f; Private floatAnitime =0.0f; Private floatAnidir =1.0f; PrivateLinerenderer Lrenderer; PublicGameobject pointer =NULL;//Little Red Dot PrivatePreframeraycast Raycast;//Ray projection voidStart () {Lrenderer= Gameobject.getcomponent (typeof(Linerenderer)) asLinerenderer; Raycast= Gameobject.getcomponent (typeof(Preframeraycast)) asPreframeraycast;//Update is called once per frame } voidUpdate () {//The light appears to be dynamic .Getcomponent<renderer> (). Material.maintextureoffset + =NewVector2 (Time.deltatime * anidir * scrollspeed,0); //Set Texture offsetGetcomponent<renderer> (). Material. Settextureoffset ("_noisetex",NewVector2 (-time.time * anidir * scrollspeed,0.0f)); floatAnifactor = Mathf.pingpong (Time.time * pulsespeed,1.0f); Anifactor= Mathf.max (MinWidth, Anifactor) *MaxWidth; //set the width of the lightlrenderer.setwidth (Anifactor, anifactor); //The beginning of light, where the muzzleLrenderer.setposition (0, This. gameObject.transform.position); if(Raycast = =NULL) {Debug.Log ("raycast is null"); return; } //Get collision information for lightRaycasthit Hitinfo =Raycast. Gethitinfo (); //the light collides with the object if(hitinfo.transform) {//The end of light, the point of collision of lightLrenderer.setposition (1, Hitinfo.point); Getcomponent<Renderer> (). Material.maintexturescale =NewVector2 (0.1f* (hitinfo.distance), getcomponent<renderer>(). Material.maintexturescale.y); Getcomponent<Renderer> (). Material. Settexturescale ("_noisetex",NewVector2 (0.1f* Hitinfo.distance *noisesize, noisesize)); if(pointer) {pointer. Getcomponent<Renderer> (). Enabled =true; //pointer.transform.position = Hitinfo.point + (transform.position-hitinfo.point) * 0.01F;Pointer.transform.position =Hitinfo.point; Pointer.transform.rotation=quaternion.lookrotation (Hitinfo.normal, transform.up); Pointer.transform.eulerAngles=NewVector3 ( -, Pointer.transform.eulerangles.y, pointer.transform.eulerangles.z); } } Else { //the light does not collide with the object if(pointer) {pointer. Getcomponent<Renderer> (). Enabled =false; } //Maximum length of light floatMaxDist =200.0f; //when the light does not collide with the object, the end point is the maximum distance in front of the muzzleLrenderer.setposition (1, ( This. Transform.forward *maxDist)); Getcomponent<Renderer> (). Material.maintexturescale =NewVector2 (0.1f* MaxDist, getcomponent<renderer>(). Material.maintexturescale.y); Getcomponent<Renderer> (). Material. Settexturescale ("_noisetex",NewVector2 (0.1f* MaxDist *noisesize, noisesize)); } }}
5. Attach the laser node to the specified position of the gun as a sub-node, the effect of the operation
About the effect of infrared sighting in unity