Hallo i have been pratecing in conveting scripts to be a better programer and i came agros a problem i can´t solve it is in a turret sript for a Tower Defense game the problem is in the last lines of my code specifik this part
for(theMuzzlePos in muzzlePositions)
{
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
}
it is the only thing i dont understand the guy that made the code(and it works in js) says that theMuzzlePos is a var and muzzlePositions is a Vector3 but the only thing that i can find that comes close to this in c# is foreach.
i am sure this is a very newbie mistake but i just can´t see what i do wrong. Sorry for the bad gramma not native english speaker.
And here is the rest of the script
using UnityEngine;
using System.Collections;
public class Turret_Connon : MonoBehaviour {
//Variabler start--------------------------------------------------------
public GameObject myProjectile;
public float reloadTime = 1f;
public float turnSpeed = 5f;
public float firePauseTime = 25f;
public GameObject muzzleEffect;
public float errorAmount = 001f;
public Transform myTarget;
public Transform[] muzzlePositions;
public Transform turretBall;
private float nextFireTime;
private float nextMoveTime;
private Quaternion desiredRotation;
private float aimError;
//Variabler end----------------------------------------------------------
// Update is called once per frame
void 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();
}
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Enemy")
{
nextFireTime = Time.time+(reloadTime*5);
myTarget = other.gameObject.transform;
}
}
void OnTriggerExit(Collider other)
{
if(other.gameObject.transform == myTarget)
{
myTarget = null;
}
}
void CalculateAimPosition(targetPos Vector3)
{
Vector3 = aimPoint(targetPos.x+aimError, targetPos.y+aimError, targetPos.z+aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}
void CalculateAimError()
{
aimError = Random.Range(-errorAmount,errorAmount);
}
void 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);
}
}
}