I am having an issue with aiming of my turret. The turret seems to be aiming and shooting right above the cube (enemy). I believe the problem is somewhere in this code.
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);
}
I tried using Quaternion.LookAt(pos) but the turret would rotate incorrectly. I give up trying to figure this out, if anyone has ideas please let me know. I just want it to shoot and hit that darn cube… . . . .
Here is my turret code if you see something else that can be changed
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-transform.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-transform.position.x+aimError, targetPos.y-transform.position.y+aimError, targetPos.z-transform.position.z+aimError);
desiredRotation = Quaternion.LookAt(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);
var go = Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
go.rigidbody.AddRelativeForce(Vector3.forward * 20000);
}
}
My guess is it's line 28 try using the transform of the target only.
– hexagonius