- Home /
I'm having problems with the FPS Tutorial MachineGun script
I put the modified PlayerWeapons script to an empty game object, and the MachineGun script to my game object I called "MachineGun", holding my gun. Before you ask what the hell am I doing, I am working on an iOS game, and I modified the PlayerWeapons script to respond on GUI Button presses. When I click on my MachineGun game object, I look at the Inspector, and the MachineGun script enables as it should,
but the problem is here: -It does not show muzzle flash -It does not show hit particles -And it does not apply damage.
Here are the scripts (JS):
The MachineGun script:
var range = 100.0;
var fireRate = 0.05;
var force = 10.0;
var damage = 5.0;
var bulletsPerClip = 40;
var clips = 20;
var reloadTime = 0.5;
private var hitParticles : ParticleEmitter;
var muzzleFlash : Renderer;
private var bulletsLeft : int = 0;
private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;
function Start () {
hitParticles = GetComponentInChildren(ParticleEmitter);
// We don't want to emit particles all the time, only when we hit something.
if (hitParticles)
hitParticles.emit = false;
bulletsLeft = bulletsPerClip;
}
function LateUpdate() {
if (muzzleFlash) {
// We shot this frame, enable the muzzle flash
if (m_LastFrameShot == Time.frameCount) {
muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
muzzleFlash.enabled = true;
if (audio) {
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
} else {
// We didn't, disable the muzzle flash
muzzleFlash.enabled = false;
enabled = false;
// Play sound
if (audio)
{
audio.loop = false;
}
}
}
}
function Fire () {
if (bulletsLeft == 0)
return;
// If there is more than one bullet between the last and this frame
// Reset the nextFireTime
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
// Keep firing until we used up the fire time
while( nextFireTime < Time.time && bulletsLeft != 0) {
FireOneShot();
nextFireTime += fireRate;
}
}
function FireOneShot () {
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
// Did we hit anything?
if (Physics.Raycast (transform.position, direction, hit, range)) {
// Apply a force to the rigidbody we hit
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
bulletsLeft--;
// Register that we shot this frame,
// so that the LateUpdate function enabled the muzzleflash renderer for one frame
m_LastFrameShot = Time.frameCount;
enabled = true;
// Reload gun in reload Time
if (bulletsLeft == 0)
Reload();
}
function Reload () {
// Wait for reload time first - then add more bullets!
yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (clips > 0) {
clips--;
bulletsLeft = bulletsPerClip;
}
}
function GetBulletsLeft () {
return bulletsLeft;
}
And the modified PlayerWeapons script:
var fireButton : GUITexture;
function Start () {
SelectWeapon(0);
}
function Update(){
for (var evt : Touch in Input.touches) {
var HitTest1 = fireButton.HitTest(evt.position);
if (evt.phase == TouchPhase.Began) {
if(HitTest1){
//do something when button is hit
BroadcastMessage("Fire");
Debug.Log("Shooting");
}
}
}
}
function SelectWeapon (index : int) {
for (var i=0;i<transform.childCount;i++) {
// Activate the selected weapon
if (i == index)
transform.GetChild(i).gameObject.SetActiveRecursively(true);
// Deactivate all other weapons
else
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
Please let me know if you find the cause of it not working.
Answer by aldonaletto · Jan 29, 2012 at 09:34 PM
You should read the FPS_Tutorial_2 document carefully: it shows the solutions for the three problems. Basically, the solutions are listed below - but is advisable to have the document above at hand for more detailed explanations:
Muzzle Flash: The machinegun has a muzzle flash object childed to it: you must click the machine gun to make it appear in the Inspector, then drag the muzzle flash to the field Muzzle Flash in the MachineGun script.
Particles: You must child the Sparks prefab to the machine gun, and the script will find it automatically.
Damage: The target objects must have a damage script attached, or nothing happens: when an object is shot, if a function ApplyDamage(damage) exists in any object script, it's called by the instruction SendMessageUpwards.
There are two different damage scripts in the FPS tutorial: DamageReceiver.js, for objects, and CharacterDamage.js, to be used in the live creatures (the robots, in this case).
where did the FPS tutorial go? i click to the link but it doesn't lead me to it :(
Apparently, Unity has removed the FPS Tutorial because it became obsolete and incompatible with Unity 4. That's a bad thing, since the FPS tutorial was very instructive.
Answer by gardian123 · Jan 04, 2013 at 08:15 PM
Rename the gun script to "MachineGun" case sensitive and make sure it's parented to the correct object. And the gun with the model should be called "machineGun" also sensitive. the weapon controller for machinegun should be "MachineGun" starting with a cap G unlike the actual model.
Your answer
Follow this Question
Related Questions
Increase energy bar by collecting item, partially working 0 Answers
Setting Scroll View Width GUILayout 1 Answer
My Lerp() is not working 2 Answers
I THINK that i have aplied a force of 1000 but it wont work 1 Answer
Make enemy more intelligent 0 Answers