I have this turret that will shoot and destroy this cube but when I duplicate this cube, the turret will only attack the original cube and not the one that was duplicated. Not sure how to fix this
Here is my script for my turret
#pragma strict
var myProjectile : GameObject;
var reloadTime : float = 1f;
var turnSpeed : float = 5f;
var firePauseTime : float = .25f;
var muzzleEffect : GameObject;
var errorAmount : float = .001;
var myTarget : Transform;
var muzzlePositions : Transform[];
var turretBall : Transform;
private var nextFireTime : float;
private var nextMoveTime : float;
private var desiredRotation : Quaternion;
private var aimError : float;
function Start ()
{
}
function Update ()
{
if(myTarget)
{
if(Time.time >= nextMoveTime)
{
CalculateAimPosition(myTarget.position);
turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed);
}
if(Time.time >= nextFireTime)
{
FireProjectile();
}
}
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "Enemy")
{
nextFireTime = Time.time+(reloadTime*.5);
myTarget = other.gameObject.transform;
}
}
function OnTriggerExit(other : Collider)
{
if(other.gameObject.transform == myTarget)
{
myTarget = null;
}
}
function CalculateAimPosition(targetPos : Vector3)
{
var aimPoint = Vector3(targetPos.x+aimError, targetPos.y+aimError, targetPos.z+aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}
function CalculateAimError()
{
aimError = Random.Range(-errorAmount, errorAmount);
}
function FireProjectile()
{
audio.Play();
nextFireTime = Time.time+reloadTime;
nextMoveTime = Time.time+firePauseTime;
CalculateAimError();
for(theMuzzlePos in muzzlePositions)
{
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
}
}
I also have script for my score which has the OnEnterCollsion function for my cube to destroy. This script is attached to my projectile
var Scores : String = "Bob";
//svar Scores : float = 0.0;
public static var scoreText:GUIText;
public static var score:int;
function Start ()
{
//intialize our variables listed
//Scores = PlayerPrefs.GetFloat("Coin");
score = 0;
scoreText = GameObject.Find("GUI_Score").GetComponent(GUIText);
//call update function to update text
UpdateScoreText();
}
function UpdateScoreText()
{
//update the GUI Text on screen
scoreText.text = "Score: " + score;
}
/*function Awake()
{
DontDestroyOnLoad(this);
}*/
function OnCollisionEnter(c:Collision)
{
Debug.Log("hit");
if (c.gameObject.name == "A Vile Cube Enemy!" )
{
Destroy(c.gameObject); // destroys the thing this script bumped into
score += 100;
UpdateScoreText();
Debug.Log("vase");
}
}
and I have a script for my projectile just in case you need to look at it
#pragma strict
var mySpeed : float = 10;
var myRange : float = 10;
private var myDist : float;
function Update ()
{
transform.Translate(Vector3.forward * Time.deltaTime * mySpeed);
myDist += Time.deltaTime * mySpeed;
if(myDist >= myRange)
Destroy(gameObject);
}