Gun Script Help (519649)

So here is my gun script but for some reason I keep getting this error.
Unknown identifier: ‘layerMask’ (line 56)
One of my friends helped me out on this script but he hasn’t been online for a while so I decided to turn here for help.

var Range : float = 1000;
var Force : float = 1000;
var Clips : int = 3;
var BulletsPerClip : int = 10;
var ReloadTime : float = 2;
var BulletsLeft : int = 0;
var ShootTimer : float = 0;
var ShootCooler : float = 0.9;

public var ShootAudio : AudioClip;
public var ReloadAudio : AudioClip;

var Damage : int = 10;
var cam : GameObject;


function Start ()
{
	BulletsLeft = BulletsPerClip;
	cam = GameObject.FindWithTag("MainCamera");
}


function Update () 
{
	if(ShootTimer > 0){
	ShootTimer -= Time.deltaTime;
	}
	
	if (ShootTimer < 0){
		ShootTimer =0;
		}


	if (Input.GetButtonDown("Fire1")  BulletsLeft){
	if(ShootTimer == 0){
	PlayShootAudio();
		RayShoot();
		
		ShootTimer = ShootCooler;
}
}
}


function RayShoot ()
{

//GameObject.Find("GunName").animation.Play("Shoot");

	var hit : RaycastHit;
	var DirectionRay = cam.transform.TransformDirection(Vector3.forward);
	var position = cam.transform.position;
	
	Debug.DrawRay(transform.position, DirectionRay * Range, Color.red);
	if (Physics.Raycast(position, DirectionRay, hit, range, layerMask.value)){
	
	if(hit.rigidbody){
	hit.rigidbody.AddForceAtPosition(DirectionRay * Force, hit.point);
	
	hit.collider.SendMessageUpwards("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
	
	
	
	}
	}
	
	BulletsLeft --;
	if (BulletsLeft < 0){
	BulletsLeft = 0;
	}
	
	if (BulletsLeft == 0){
	Reload();
	}
	
}

function Reload ()
{

//GameObject.Find("GunName").animation.Play("Reload");

	PlayReloadAudio();
	yield WaitForSeconds(ReloadTime);
	
	if (Clips > 0){
	BulletsLeft = BulletsPerClip;
	Clips -= 1;
}
}

function PlayShootAudio ()
{
	audio.PlayOneShot(ShootAudio);
}

function PlayReloadAudio ()
{
	audio.PlayOneShot(ReloadAudio);
}

Like the error says

layerMask cannot be identified :slight_smile:

line 56 of your code

if (Physics.Raycast(position, DirectionRay, hit, range, layerMask.value))

layerMask is not a variable on your script. :slight_smile:

EDIT:
Check this doc out so you know what the layer mask value is

if you don’t like too much reading, layer mask is the ID of the layer that you want to ignore when doing the RayCasting. Each game object has a tag and a layer associated with it and the layers are used for collisions.

hope it helps. :slight_smile:

What would I set the variable to then? That part of the script was originally made to make the gun shoot towards the center of the screen (the crosshair). If you could help me with that then it would be awesome.

Check the layer number of the crosshair game object.

if you select the crosshair game object and go to the Inspector → Layer → (drop down) Add Layer.

look for the crosshair name layer and look at the number, then use that as the layer mask value. :slight_smile:

with that, Raycasting should ignore the crosshair game object.

The crosshair is a script attached to the Main Camera, should I use the Main Camera layer or make a separate GameObject parented under the Main Camera for the crosshair script?

if the crosshair is being drawn within a script then you don’t need to worry about the layerMask. you can do it like this.

if (Physics.Raycast(position, DirectionRay, hit, range)

but if that crosshair script is instantiating a game object then look for the game object being instantiated, probably on your prefabs folder then again do the layer number check. :slight_smile:

EDIT:
post the crosshair script here then I might be able to help :slight_smile:

EDIT 2:
if it doesn’t work then use the layer of the MainCamera as layer mask and try

Oh great, now I get this error

RayShoot.js(61,28): BCE0023: No appropriate version of ‘UnityEngine.Physics.Raycast’ for the argument list ‘(UnityEngine.Vector3, UnityEngine.Vector3, UnityEnging.RaycastHit, function(int): System.Collections.Generic.IEnumerable.)’ was found.

ignore the first solution. it was my fault. check my other edits

or if you’re willing to show the crosshair script then much better. :slight_smile:

Alright here is the crosshair script. The script is not mine but was made by OMA and he gave it out free, pretty much the only dynamic crosshair I could get my hands on.

Also, do you have a Skype? This thing is killing me lol.

@script ExecuteInEditMode()
enum preset { none, shotgunPreset, crysisPreset }
var crosshairPreset : preset = preset.none;
 
var showCrosshair : boolean = true;
var verticalTexture : Texture;
var horizontalTexture : Texture;
 
//Size of boxes
var cLength : float = 10;
var cWidth : float = 3;
 
//Spreed setup
var minSpread : float = 45.0;
var maxSpread : float = 250.0;
var spreadPerSecond : float = 150.0;
 
//Rotation
var rotAngle : float = 0.0;
var rotSpeed : float = 0.0;
 
private var temp : Texture;
private var spread : float;
 
function Update(){
        //Used just for test (weapon script should change spread).
        if(Input.GetKey(KeyCode.K)) spread += spreadPerSecond * Time.deltaTime;
        else spread -= spreadPerSecond * 2 * Time.deltaTime;   
       
        //Rotation
        rotAngle += rotSpeed * Time.deltaTime;
}
 
function OnGUI(){
        if(showCrosshair  verticalTexture  horizontalTexture){
                var verticalT = GUIStyle();
                var horizontalT = GUIStyle();
                verticalT.normal.background = verticalTexture;
                horizontalT.normal.background = horizontalTexture;
                spread = Mathf.Clamp(spread, minSpread, maxSpread);
                var pivot : Vector2 = Vector2(Screen.width/2, Screen.height/2);
               
                if(crosshairPreset == preset.crysisPreset){
                       
                        GUI.Box(Rect((Screen.width - 2)/2, (Screen.height - spread)/2 - 14, 2, 14), temp, horizontalT);
                        GUIUtility.RotateAroundPivot(45,pivot);
                        GUI.Box(Rect((Screen.width + spread)/2, (Screen.height - 2)/2, 14, 2), temp, verticalT);
                        GUIUtility.RotateAroundPivot(0,pivot);
                        GUI.Box(Rect((Screen.width - 2)/2, (Screen.height + spread)/2, 2, 14), temp, horizontalT);
                }
               
                if(crosshairPreset == preset.shotgunPreset){
               
                        GUIUtility.RotateAroundPivot(45,pivot);
                       
                        //Horizontal
                        GUI.Box(Rect((Screen.width - 14)/2, (Screen.height - spread)/2 - 3, 14, 3), temp, horizontalT);
                        GUI.Box(Rect((Screen.width - 14)/2, (Screen.height + spread)/2, 14, 3), temp, horizontalT);
                        //Vertical
                        GUI.Box(Rect((Screen.width - spread)/2 - 3, (Screen.height - 14)/2, 3, 14), temp, verticalT);
                        GUI.Box(Rect((Screen.width + spread)/2, (Screen.height - 14)/2, 3, 14), temp, verticalT);
                }
               
                if(crosshairPreset == preset.none){
               
                        GUIUtility.RotateAroundPivot(rotAngle%360,pivot);
                       
                        //Horizontal
                        GUI.Box(Rect((Screen.width - cWidth)/2, (Screen.height - spread)/2 - cLength, cWidth, cLength), temp, horizontalT);
                        GUI.Box(Rect((Screen.width - cWidth)/2, (Screen.height + spread)/2, cWidth, cLength), temp, horizontalT);
                        //Vertical
                        GUI.Box(Rect((Screen.width - spread)/2 - cLength, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
                        GUI.Box(Rect((Screen.width + spread)/2, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
                }
        }
}

Ok that clears it up. :slight_smile: use the Main Camera Layer as the layer mask value.

I assume its on default? or if not then you guys made a layer for it.

if it doesn’t work then, catch me online in 4 hours :slight_smile: sleeping in 30 minutes.

necther.louie.souribio ← skype