FPS Bullets Generic Solution. How?

Im designing a game to learn… Im a hobbyist.

The game is very simple the screen will show you objects and you shoot it.

Thats been said.
What i need to achive is this.

1- FPS moves around (Solved using FPS script with its mouse controller script)
2- Add Cross-hair in the screen for aiming (Solved using the following code)
I added it to the FPS Camera

    var crosshairTexture : Texture2D;
    var position : Rect;
    static var OriginalOn = true;
     
    function Start()
    {
    position = Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height -
    crosshairTexture.height) /3, crosshairTexture.width, crosshairTexture.height);
    }
     
    function OnGUI()
    {
    if(OriginalOn == true)
    {
    GUI.DrawTexture(position, crosshairTexture);
    }
    }

3- Bullets shoot i used the bellow script

var fireRate : float = 4.0;
private var nextFire : float = 0.0;
var prefabBullets:Transform;
var shootForce: float;
    function Start(){   
		}                          
    function Update () {
    
    	if (Input.GetMouseButtonDown(0) && IsOkToShoot()) {
    		Shoot(); //Shoot when conditions are met
    		var instanceBullet = Instantiate(prefabBullets, transform.position, Quaternion.identity);
    		instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
    		Destroy (instanceBullet.gameObject, 2); 
    	}
    }
     
    function IsOkToShoot () : boolean {
    //Here we check that current time exceeds nextFire
    var itsOk : boolean = false;
    
	    if (Time.time>nextFire) {
	    	//Debug.Log(Time.time);
	    	nextFire = Time.time + fireRate;
	    	itsOk = true;
	    }
    return itsOk;
    }
     
    function Shoot () {
audio.Play();
    }

I have few questions…

1- How to make the bullets Always go at the Cross-hair ?
2- How to Get the objects name that the bullets hit ?

Any help it would be great thank you all…

Hello There,

Generally in FPS games, the bullets fired from your gun are not real 3d projectiles that hit your target, but rather just a graphical asset ,there are exceptions to this, but those involve a whole physics solution for the projectile, involving wind forces, for example.

In this case, I would recommend using the most usual solution. To achieve that, instead of instatiating a bullet prefab and adding force to it, simply cast a Raycast from your camera to check if you did hit something.

Unity’s docs on this : Original Docs

Here is a small example on achieving this:

  1. LayerMask : Create a Layer for all the objects that can be hit
  2. Camera.main.ScreenPointToRay() Will create a ray going for the FPS camera to the crosshair ( or you could just use the Input.mousePosition )

var hittableObjects : LayerMask;
var ray = Camera.main.ScreenPointToRay (crossHairPosition);
var hit : RaycastHit;
var range : float = 100;

if (Physics.Raycast (ray, hit, range, hittableObjects)) {
   // You have hit something
}

Hope it helps.

Hear is a simple code
But it still need alot of tweeking
Such as

-When object is clicked on a particulare animation of that object works…

  • Sound for shoot and for reload (I tried playing 2 sound but its just not working. I will check with Unity3d Answers.)
  • …etc

New Updated CODE
Please Comment on it for improvement…

#pragma strict
@script RequireComponent(AudioSource)
@script RequireComponent(AudioSource)
var animatedObject1 : GameObject;
var animatedObject2 : GameObject;

var Range : float = 50;
var Force : float = 400;

var fireRate : float = 2.0; //The rate of fire for the gun
private var nextFire : float = 0.0; //When the next fire is ok to shoot off

public var itsOk : boolean = true;

var audio1: AudioSource;
var audio2: AudioSource;


function Start(){
    var aSources = GetComponents(AudioSource);
    audio1 = aSources[0];
    audio2 = aSources[1]; 
}

function Update () {
		if (Input.GetButtonDown("Fire1") && itsOk && IsOkToShoot()){
			audio2.Play();
			ShootGun();
			itsOk = false;
		}

		if(Input.GetButtonDown("Fire2") && itsOk == false){
			ReloadLogic();
			itsOk = true;
		}
}
function  ReloadLogic() : boolean{
	var itsOk : boolean = false;
	PlayReloadAudio ();
	ReloadDelay();// This Does not seem to work therfore i used IsOkToShoot()
	itsOk = true;
			
	return itsOk;
}

function ReloadDelay(){

yield WaitForSeconds(1.5);

}

function ShootGun (){

	var Hit: RaycastHit;
	var DirectionRay = transform.TransformDirection(Vector3.forward);
	
	Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue );
	
	if(Physics.Raycast(transform.position, DirectionRay, Hit, Range)){
	
		if(Hit.rigidbody){
			Hit.rigidbody.AddForceAtPosition(DirectionRay * Force, Hit.point);
			 
			 var distanceToGround = Hit.distance;
			 var HitName = Hit.transform.gameObject.name;
			 var HittedObjectName = Hit.transform.gameObject;
			 // Some Debuging
			 print(distanceToGround);
			 print(HitName);
			 print(animatedObject1.name); 
			 
			 if(animatedObject1.name == HitName){ 
			 	animatedObject1.animation.Play("die");

			 }else if(animatedObject2.name == HitName){
			 	animatedObject2.animation.Play("diehard"); 
 
			 }
			 
			 
			 Destroy(HittedObjectName, 5);
		}
	}
}

   function IsOkToShoot () : boolean {
    //Here we check that current time exceeds nextFire
    var itsOk : boolean = false;
    
	    if (Time.time>nextFire) {
	    	//Debug.Log(Time.time);
	    	nextFire = Time.time + fireRate;
	    	itsOk = true;
	    }
    return itsOk;
    }

function PlayShootAudio (){
	audio2.Play();
}
function PlayReloadAudio (){
audio1.Play();
}