top of page

Basic Sniper Code in Unity

Have you ever sniped? And wondered how do do that? Hmm, This blog is for you. Here I am gonna create a basic sniper mechanics, and you can add your own features further.


Have a glance at this final gameplay before we start.

Firstly, for the first person controls - Import the package "FirstPerson AIO Pack" from the asset store. To that first person attach sniper model( can also be imported from asset store).


Set the gun position. Now, create an animation which is played when we click the right mouse button.

Firstly create Ideal 'breathe' animation.And then scoped animation which we transfer from ideal to scoped animation on right mouse button click.

Attach the below code to your gun model which you have attached to FPS.


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;


public class SniperControl : MonoBehaviour

{

Animator animator; // For the animation that happends when you right click for the scope.

public Camera fpsCamera; //Fps Camera

public GameObject sniperADS; //Sniper scope. Basically this is a pannel with scope cross hair image.



RaycastHit hit;


// Start is called before the first frame update

void Start()

{

animator = GetComponent<Animator>();

sniperADS.SetActive(false); // initially you dont see the sniper cross hair scope. This should get activated once you click on the right mouse button.

}


// Update is called once per frame

void Update()

{

SniperADSFunction();

}





void SniperADSFunction()

{

if (Input.GetKey(KeyCode.Mouse1))

{

animator.SetBool("ADS", true); //Play that aiming animation once you right click

fpsCamera.fieldOfView = 10.0f; //Set field of view to 10, to have that zoom effect

Invoke("ActivateSniperADS", 0.2f); // after 2 seconds, Give that cross hair zoom on the screen by activating the cross hair image.

if(Input.GetKeyDown(KeyCode.Mouse0))

{

FireSnipper(); //For firing by ray tracing from camera to mid screen

Recoil(); //Recoil of Snipper

}

}

else

{

animator.SetBool("ADS", false); // Once you release right mouse button, come back to original position.

fpsCamera.fieldOfView = 60.0f; //Reset field of view back to normal

CancelInvoke("ActivateSniperADS");

GetComponent<MeshRenderer>().enabled = true;

sniperADS.SetActive(false);

}

}


void ActivateSniperADS()

{

GetComponent<MeshRenderer>().enabled = false; // Make the gun vanish

sniperADS.SetActive(true);

}



void FireSnipper() // Creating a ray cast from camera to mid screen.

{

Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

if (Physics.Raycast(ray, out hit))

{

Debug.Log(hit.transform.gameObject);

if (hit.transform.gameObject.GetComponent<Rigidbody>())

hit.transform.gameObject.GetComponent<Rigidbody>().AddForce(-hit.normal * Random.Range(200,400),ForceMode.Impulse);

}

}



void Recoil()

{

GetComponentInParent<FirstPersonAIO>().enabled = false;

Debug.Log("RECOIL");

Vector3 curr = fpsCamera.transform.localRotation.eulerAngles;

fpsCamera.transform.localRotation = Quaternion.Euler(curr + new Vector3(-5.0f, 0, 0));

StartCoroutine("RecoilHelp");

}



IEnumerator RecoilHelp()

{

yield return new WaitForSeconds(0.3f);

GetComponentInParent<FirstPersonAIO>().enabled = true;

}

}




Each code is commented which is self explanatory.






 
 
 

Recent Posts

See All

Comments


bottom of page