Using the Procedural project, lighting example:
I am trying to make a lighting gun, similar in behavior with the link gun from UT.
Basically, my weapon should pick up the target I aim and draw the particles from my gun to that target.
My problem is how to assign the target in the gun’s variable since it’s a prefab (and not the only one on screen).
Any ideas, links to similar issues, greatly appreciated, thanks!
var currentTarget : Transform = null;
var weaponRange : float = 20.0;
function Update()
{
if (Input.GetMouseButton(0))
{
if (currentTarget == null)
{
var ray = Camera.ViewportPointToRay(Vector3(0.5,0.5,0));
var hit : RaycastHit;
if (Physics.Raycast(ray,hit,weaponRange))
{
if (hit.transform.tag == “Player”)
{
currentTarget = hit.transform;
}
else
{
// maybe do normal damage to other objects you can’t lock on.
// hit.transform refers to the object you hit
}
}
}
else
{
// We are locked on a target
var vectorTotarget = currentTarget.position - transform.position;
if (vectorTotarget.sqrMagnitude > weaponRange*weaponRange) // is the target out of range?
{
currentTarget = null; // clear target
}
else
{
// Do damage to the target
}
}
}
else if(Input.GetMouseButtonUp(0))
{
// when you release the fire button clear the target
currentTarget = null;
}
}
This script will shoot like every normal weapon, straight forward. If it hits a gmaeObject that is tagged “Player” it will lock to this target until you release the fire button.
It’s just the concept. You have to change/extend it to your needs…
Once you put the prefab in the scene, an instance of it is created. Everything in this prefab is cloned to create this instance, including the scripts and their variables (except those declared as static - they are global), so you can have different values assigned to variables located in different instances of the same prefab.
TEXT ADDED TO THE ANSWER
Welcome, left brainer! Let’s help your right side:
1- When the script spawns the enemies, it creates separated instances of them too, so they will be as independent as if you created them in the editor - every object in the game is an indepenent instance, no matter how it was created.
2- You can cast a ray from your camera to the center of the screen, which in principle is the point you will be aiming to. The script below was adapted from:
http://unity3d.com/support/documentation/ScriptReference/Camera.ViewportPointToRay.html
It just draws a line from the character to the point the camera is aiming to. I shifted the line origin downside 0.5m, or just a red point would appear in the center of the screen. You must also click the Gizmo button on the tool bar (above the Scene/Game window), or the line will not be drawn in the Game view. Don’t get excited with this red ray: it will not appear in the final game, so forget about using Debug.DrawLine to create a cheap laser aim!
function Update() {
var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
// Do a raycast
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)){
var gunPos:Vector3 = transform.position;
gunPos.y -= 0.5;
Debug.DrawLine(gunPos,hit.point,Color.red);
}
}
I tried something - well, various avenues) and I keep bumping into C# and Js - I know a bit more of the latter and the original perlin noise lighting script is in C# so I am not sure what I am doing. Here’s what I did so far - it doesn’t seem to do anything but at least I am not getting any errors. Initially - since the gun is a child object of the Camera - I got some errors there, fixed with camera changed to Camera.main. Clear head in the morning may let me see this in a more optimistic light 
using UnityEngine;
using System.Collections;
public class LightningGun : MonoBehaviour
{
public Transform target = null;
public int zigs = 100;
public float speed = 1f;
public float weaponRange = 20f;
public float scale = 1f;
public Light startLight;
public Light endLight;
public AudioClip projectileSound;
Perlin noise;
float oneOverZigs;
private Particle[] particles;
void Start()
{
oneOverZigs = 1f / (float)zigs;
particleEmitter.emit = false;
particleEmitter.Emit(zigs);
particles = particleEmitter.particles;
}
void Fire ()
{
if (target == null)
{
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, weaponRange))
if (hit.transform.tag == "EnemyShip")
{
target = hit.transform;
if (noise == null)
noise = new Perlin();
float timex = Time.time * speed * 0.1365143f;
float timey = Time.time * speed * 1.21688f;
float timez = Time.time * speed * 2.5564f;
for (int i=0; i < particles.Length; i++)
{
Vector3 position = Vector3.Lerp(transform.position, target.position, oneOverZigs * (float)i);
Vector3 offset = new Vector3(noise.Noise(timex + position.x, timex + position.y, timex + position.z),
noise.Noise(timey + position.x, timey + position.y, timey + position.z),
noise.Noise(timez + position.x, timez + position.y, timez + position.z));
position += (offset * scale * ((float)i * oneOverZigs));
particles*.position = position;*
_ particles*.color = Color.white;_
_ particles.energy = 1f;
}
audio.clip = projectileSound;
audio.Play();
particleEmitter.particles = particles;*_
* if (particleEmitter.particleCount >= 2)*
* {*
* if (startLight)*
* startLight.transform.position = particles[0].position;*
* if (endLight)*
* endLight.transform.position = particles[particles.Length - 1].position;*
*} *
* }*
else
target = null;
* }*
* } *
}