Turret tracks enemy but doesn’t fire the projectile.
Turret Script __________________________
#pragma strict
var myProjectile : GameObject;
var reloadTime : float = 1f;
var turnSpeed : float = 5f;
var firePauseTime : float = .25f;
var errorAmount : float = .001;
var myTarget : Transform;
var muzzlePosition : 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;
}
}
{
if(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()
{
nextFireTime = Time.time+reloadTime;
nextMoveTime = Time.time+firePauseTime;
CalculateAimError();
}
Projectile Script __________________________________________________________________
#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);
}
[code ] [/code ] tags (without the spaces) help when you paste in code chunks… makes is far more readable => easier to help you.
I don’t see an Instantiate call at any point…
As @LeftyRighty said, you aren’t instantiating anything.
#pragma strict
var myProjectile : GameObject;
var reloadTime : float = 1f;
var turnSpeed : float = 5f;
var firePauseTime : float = .25f;
var errorAmount : float = .001;
var myTarget : Transform;
var muzzlePosition : 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;
}
}
{
if(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()
{
nextFireTime = Time.time+reloadTime;
nextMoveTime = Time.time+firePauseTime;
CalculateAimError();
for(theMuzzlePos in muzzlePosition)
{
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
}
}
I added the instantiate and it still doesn’t shoot I am very new to coding so I don’t know much about it.
not sure what you are trying to do here:
for(theMuzzlePos in muzzlePosition)
{
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
}
but it should read something like this:
foreach(Transform theMuzzlePos in muzzlePosition)
{
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
}
assuming you have your muzzlePosition array filled out
i tried what you suggested and it did not work it creates an error
need to know the error or see code to figure out whats going on here
Think the problem is here:
if(gameObject.transform == myTarget)
{
myTarget = null;
}
It is included inside the OnTriggerEnter event and making myTarget to always = null.
Needs to be changed to
funtion OnTriggerExit(other : Collider)
{
if(gameObject.transform == myTarget)
{
myTarget = null;
}
}
if(gameObject.transform == myTarget)
{
myTarget = null;
}
I believe the small g in gameObject.transform is checking to make sure the turret isn’t targeting itself… if it is set myTarget to null. So as long as the turret isn’t targeting itself I think that line should be OK.
Also he said the turret tracks the target OK… so myTarget must be good.
Without the error its hard to say what is wrong… I’d guess he doesn’t have any muzzle transforms in his muzzleposition array.
on a side note… Why do you use aimError while tracking your target but only update it just before a shot is supposed to be fired
I’m doing this for a school project and I’m just following a tutorial this is my first time scripting anything.
Both the Turret and the Projectile code are in the post and there is no error that appears
Her is the updated code still doesn’t shoot
#pragma strict
var myProjectile : GameObject;
var reloadTime : float = 1f;
var turnSpeed : float = 5f;
var firePauseTime : float = 0.25f;
var muzzleEffect : GameObject;
var errorAmount : float = 0.001;
var myTarget : Transform;
var muzzlePositions : Transform[];
var turretTurn : 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);
turretTurn.rotation = Quaternion.Lerp(turretTurn.rotation, desiredRotation, Time.deltaTime*turnSpeed);
}
if(Time.time >= nextFireTime){
FireProjectile();
}
}
}
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "Enemy"){
nextFireTime = Time.time+(reloadTime*0.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 - transform.position.x + aimError, targetPos.y - transform.position.y + aimError, targetPos.z - transform.position.z + aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}
function CalculateAimError(){
aimError = Random.Range(-errorAmount, errorAmount);
}
function FireProjectile(){
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);
}
}
this part is still wrong
function FireProjectile(){
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);
}
}
it should be more like:
function FireProjectile(){
nextFireTime = Time.time + reloadTime;
nextMoveTime = Time.time + firePauseTime;
CalculateAimError();
foreach(Transform theMuzzlePos in muzzlePositions){
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
}
}
Also, what are you using for a projectile? Does it have it’s own script attached? How does the projectile move? If there isn’t a script that moves the projectile attached to the projectile your projectiles will just be spawned and sit at the muzzle point (and depending how those are set up you might be spawning bullets that aren’t moving hidden behind/inside some
novashot:
this part is still wrong
function FireProjectile(){
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);
}
}
it should be more like:
function FireProjectile(){
nextFireTime = Time.time + reloadTime;
nextMoveTime = Time.time + firePauseTime;
CalculateAimError();
foreach(Transform theMuzzlePos in muzzlePositions){
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
}
}
Also, what are you using for a projectile? Does it have it’s own script attached? How does the projectile move? If there isn’t a script that moves the projectile attached to the projectile your projectiles will just be spawned and sit at the muzzle point (and depending how those are set up you might be spawning bullets that aren’t moving hidden behind/inside some
I am just using a sphere with the script that is in the thread.
wondyr
May 8, 2014, 12:48am
18
Gotta ask. Have you created the Tag Enemy and assigned it to the enemy object?
i do have the enemy named enemy i don’t know if that is the tag.
the turret tracks perfectly fine it just doesn’t shoot so i’m guessing it is tagged as enemy since it targets it.
these errors comes up when i add what novashot suggested.
Assets/Turret.js(64,27): BCE0044: expecting ), found ‘theMuzzlePos’.
Assets/Turret.js(64,58 ): BCE0043: Unexpected token: ).