- Home /
FPS Scripting Problems
Okay, for some reason my machinegun doesnt have a projectile or it doesnt do anything other than shoot(not even apply damage, here is my code:
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 (Input.GetButtonDown("Fire1")) {
// Instantiate the projectile at the position and rotation of this transform
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.forward * speed);
}
// 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 0) {
clips--;
bulletsLeft = bulletsPerClip;
}
}
function GetBulletsLeft () { return bulletsLeft; }
Answer by spinaljack · Jun 07, 2010 at 12:06 PM
Seeing as damage is declared but never used in your code example it must mean that it's being applied in another script (if at all). Try posting that one instead.
Assuming the apply damage script is on the projectile you need to make sure how it's detecting collisions (colliders, triggers or rays).
Then you need to make sure that it's detecting collisions, you can use a print or a debug log to say when it detects a collision
If it isn't triggering properly you need to make sure you've got the right type of colliders. If the bullet and the target both have non-convex mesh colliders then they wont ever collide. One of the objects needs to have a non-kinematic rigidbody (probably the projectile)
Then check that it's sending the damage message to the target and then you need to make sure that the target can take damage. Give it a script called ApplyDamage and reduce its health accordingly
etc. etc.
You simply need to read the FPS tutorial through carefully to get a fully functioning shooter game.
Your answer
Follow this Question
Related Questions
Gun Manager only working once 1 Answer
Camera Recoil,Please help me. 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
C# How to have weapon pickup 1 Answer
gun bob and jerk animation 2 Answers