- Home /
Automated turret targets around origin, regardless of location
Basically I've created a fairly simple automated turret, and I've just discovered that if I move said turret from its position at the origin, it continue to target as if it was still at the origin.
So, imagine you were looking down upon the turret and its surroundings. If I move the turret 5 units to the right, and place an enemy 5 units blow the origin (bare in mind this is relative to the viewpoint), the turret should aim south west, at the target, but instead it aims straight down, as if it's still positioned at the origin.
I've gone over my code and can't identify anything that would cause this, but then again I'm not particularly experienced. Apologies for any inefficiencies in the code, it's the result of a lot of trial, error and fiddling, but here it is:
#pragma strict
var myProjectile : GameObject;
var reloadTime : float = 0.5f;
var turnSpeed : float = 5f;
var firePauseTime : float = 0.25f;
var muzzleEffect : GameObject;
var errorAmount : float = 0.1;
var turretBall : Transform;
var resetDelay : float =3;
var resetTurnSpeed : float = 3;
var laserGlow : Light;
var laserBeam : Light;
var leftMuzzlePos : Transform;
var rightMuzzlePos : Transform;
var stepAmount : float = 10;
private var myTarget : Transform;
private var nextFireTime : float;
private var nextMoveTime : float;
private var desiredRotation : Quaternion;
private var aimError : float;
private var originalPos : Quaternion;
private var resetTime : float;
private var laserGlowSegments : float;
private var laserBeamSegments : float;
function Start ()
{
originalPos = turretBall.rotation;
laserGlow.enabled = false;
laserBeam.enabled = false;
}
function Update ()
{
if(myTarget)
{
calculateAimPosition(myTarget.position);
turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * turnSpeed);
if(Time.time >= nextFireTime)
{
FireProjectile();
}
}
else
{
if(Time.time >= resetTime)
{
turretBall.rotation = Quaternion.Lerp(turretBall.rotation, originalPos, resetTurnSpeed * 0.01);
laserGlow.enabled = false;
laserBeam.enabled = false;
}
}
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.CompareTag ("Enemy"))
{
nextFireTime = Time.time+reloadTime;
myTarget = other.gameObject.transform;
laserGlow.enabled = true;
laserBeam.enabled = true;
}
}
function OnTriggerExit(other : Collider)
{
if(other.gameObject.transform == myTarget)
{
resetTime = Time.time+resetDelay;
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();
Instantiate(myProjectile, leftMuzzlePos.position, leftMuzzlePos.rotation);
Instantiate(myProjectile, rightMuzzlePos.position, rightMuzzlePos.rotation);
}
function FadeLightOut()
{
for(var step = stepAmount; step >= 0; step--)
{
laserGlow.intensity = laserGlow.intensity - (0.8/stepAmount);
laserBeam.intensity = laserBeam.intensity - (0.8/stepAmount);
}
}
I think he bit of code that's causing it is the calculateAimPosition function, but I could be wrong. Thanks for any help.
Answer by perchik · Aug 02, 2013 at 04:56 PM
First, for clarity, originalPos
should be renamed originalRot
, just so your code makes sense.
Perhaps try changing
calculateAimPosition(myTarget.position);
to
calculateAimPosition(transform.position-myTarget.position);
That should calculate the position to the relative target, not it's absolute position
Good point about the variable name, I've changed that.
However, your other change causes the turret to point in the direction directly opposite the target.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111428im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Turret aiming script needs adjustment / fine tuning 1 Answer
first Gun fires not well 2 Answers
Autoaiming Turret Cannon? 3 Answers
Turret + Cannon Mouse movement 1 Answer
Turrets |Aiming don't work 1 Answer