Weapon Pick Up And Hit Particle

Hey guys I have 2 questions to ask so I though I would get them over with in 1 shot… ok so here are my qustions

  1. How would I make a weapon able to pickup and put into my primary or secondary slot in my weapon switch code. Also how would I make it display “Press F To Pickup” When you go near it.
  2. How would I incorporate hit particles to whatever I hit. Like say I shoot at metal is shows sparks I have the first part done with different bullet textures

Here are my script’s

Weapon Switch- Weapon Switch - Pastebin.com
Bullet Script- Bullet Script - Pastebin.com

Thanks guys


Picking weapons, ammunition and other goods is usually a fake operation: you place only the model with a trigger volume in scene; when the player enters the trigger, destroy the pickable item and “add” it to your inventory system, whatever it is - when picking bullet clips, for instance, increment the variable “remainingBullets” or equivalent that you already have in your weapon script; when picking weapons, usually you just set some enable variable that controls the weapon in your character (the weapon is actually added to the character in the Editor, but can’t be selected until its control variable is true). That’s an example of a picking script attached to the player:

function OnTriggerEnter(other: Collider){
  var pickup = true;
  switch (other.tag){
    case "Rocket": 
      rocketScript.rockets++; // add to rocket ammunition
    case "AmmoClip": 
      machGunScript.ammo += machGunScript.bulletsPerClip;
    case "RocketLauncher":
      weaponsScript.hasRocketLauncher = true; // enable rocket launcher selection
    case "MachineGun":
      weaponsScript.hasMachineGun = true; // enable machine gun selection
      pickup = false; // pick nothing
  if (pickup) Destroy(other.gameObject); // destroy item 

The “press F to pick” message is just a detail in your pickup script: when the item trigger is entered, display this message and watch for the F key to effectively destroy it and add the item to your inventory.

About particles: the most versatile way is to create a “Particles” script and add it to the object prefabs, selecting the particles in the Inspector for each case; when hitting an object, try GetComponent(Particles) - if the object has the Particles script, call the function EmitAt(hit.point, hit.normal); if it doesn’t have such script, do nothing or emit a default particle effect.

var particlePrefab: ParticleEmitter;
private var particle: ParticleEmitter;

function Start(){
  particle = Instantiate(particlePrefab); // create the particles...
  particle.emit = false; // but keep it inactive 

function EmitAt(hitPoint: Vector3, hitNormal: Vector3){
  particle.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitNormal);
  particle.transform.position = hitPoint;
  particle.Emit(); // emit particles once