- Home /
Problem With Weapon Firing Mechanism
Hi, I am making a weapon system for a FPS game whatever that might be. I have the basic structure in place to have a player that can pickup weapons and toggle between them. All weapons have a different magazine (in some cases some weapons may share the same type of magazine) and each magazine contains bullets based on the gun the magazine is loaded into. I also have a "FirePoint" game object attached to the camera on the first person controller as well as a bullet prefab (a simple sphere with a rigid body attached).I have even created the logic that works out when to fire the weapon and when not to and that all works.
However, when I fire to the left or right the trajectory of the bullet doesn't update with the new transform of the player. Here is a picture of my scene hierarchy http://i981.photobucket.com/albums/ae300/martianxx/Heiarachy_zps7599365c.jpg (sorry for bad quality). Here is the code I use when firing.
//Player.js in update function
if (Input.GetKey(KeyCode.Mouse0))
{
Debug.Log(equipment.GetWeapon(equipment.GetCurWeapon()).canFireNonAuto);
if (equipment.GetWeapon(equipment.GetCurWeapon()).canFire && equipment.GetWeapon(equipment.GetCurWeapon()).canFireNonAuto)
{
StartCoroutine(equipment.GetWeapon(equipment.GetCurWeapon()).Fire());
}
}
if (Input.GetKeyUp(KeyCode.Mouse0))
{
equipment.GetWeapon(equipment.GetCurWeapon()).CeaseFire();
equipment.GetWeapon(equipment.GetCurWeapon()).canFireNonAuto = true;
}
//Pistol.js
function Fire() : IEnumerator
{
canFire = false;
canFireNonAuto = false;
var track : GameObject;
track = GameObject.Instantiate(ammo.bullet.bullet, weaponFirePoint.transform.position, weaponFirePoint.transform.rotation);
track.rigidbody.AddForce(track.transform.forward * 10000);
yield WaitForSeconds(timeToNextShot);
canFire = true;
}
//AssaultRifle.js
function Fire() : IEnumerator
{
canFire = false;
var track : GameObject;
track = GameObject.Instantiate(ammo.bullet.bullet, weaponFirePoint.transform.position, weaponFirePoint.transform.rotation);
track.rigidbody.AddForce(track.transform.forward * 10000);
yield WaitForSeconds(timeToNextShot);
canFire = true;
canFireNonAuto = true; //its an automatic weapon so set this true here to overide the non automatic logic would be better to have an automatic class
}
//AssaultRifle.js example weapon constructor
function AssaultRifle()
{
ID = WeaponID.ARIFLE;
weaponName = "Assault Rifle";
weaponIcon = Resources.Load("AssaultRifle", typeof(Texture2D));
weaponFirePoint = GameObject.FindWithTag("FirePoint");
ammo = new Magazine(32, BulletID.ARIFLE);
rateOfFire = 60;
timeToNextShot = 0.1;
reload = false;
reloadTime = 2.0;
}
is your bullet spawning in the wrong spot OR is it going the wrong direction?
$$anonymous$$y guess is its "forward" isn't parallel with the barrel.
if your bullet is spawning at the right location but with the wrong direction you could try rotating the firing point or you could in the direction of gun.forward maybe?
Yeah its defiantly because "forward" is not parallel with the "barrel". Currently I don't have any weapon mesh to represent the various weapons. The bullet defiantly spawns at the correct location. Can I have another game object attached to the camera that the FirePoint game object rotates towards OR work out the vector between the two, normalize it and then use that ins$$anonymous$$d of forward? Which would be better or is there another better option?
In debug mode in scene view I can see that the positive z direction is the vector I want to fire along.
I made a simple script as shown below and it confirms that the forward is not in the correct direction.
void OnDrawGizmos()
{
Gizmos.DrawLine(transform.position, transform.forward);
}
This is attached to the FirePoint game object
Answer by TutiBueno2 · Jan 20, 2013 at 10:56 PM
Is it feasible for you if you use the rotation of the camera instead?
Like this:
track = GameObject.Instantiate(ammo.bullet.bullet, weaponFirePoint.transform.position, Camera.main.transform.rotation);
Since in FPS games the camera is always aligned with what the player is viewing, this will (hopefully) work.
Sorry for the trouble, I was being an idiot. Had sphere collider attached to the game object i was spawning a rigid body at >.<
Your answer
Follow this Question
Related Questions
Weapon Switch Animations 1 Answer
Weapon Switching 2 Answers
FPS Weapon Animation Mixing 1 Answer
Checkpoint script? 1 Answer
force player to crouch when under an object with collisio 2 Answers