- Home /
Issues with twin cannon script
I was following along a tower defense tutorial I came across here;
And I'm experience a bug I can't seem to explain. I have my turret working absolutely wonderful, but only if it's at 0, 0, 0. The moment the turrent is moved off of 0, it bugs out.
I've been very careful to set all my pivots correctly as the tutorial has theirs. ( in local space ) But something is clearly off here.
here's my script;
#pragma strict
var reloadTime : float = 1.0;
var turnSpeed : float = 5.0;
var errorAmount : float = .001;
var firePauseTime : float = .25;
var gizmoRadius : float = 4.0;
var muzzleEffect : GameObject;
var myProjectile : GameObject;
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 CalculateAimError ()
{
aimError = Random.Range ( -errorAmount, errorAmount );
}
function CalculateAimPosition ( targetPos : Vector3 )
{
var aimPoint = Vector3 ( targetPos.x + aimError, targetPos.y + aimError, targetPos.z + aimError );
desiredRotation = Quaternion.LookRotation ( aimPoint );
}
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 );
}
}
function OnDrawGizmos ()
{
Gizmos.color = Color.white;
Gizmos.DrawWireSphere ( transform.position, gizmoRadius );
}
of course any help I can get would be greatly appreciated! thanks guys!
There's probably a reason for that, but why aren't you using the lookAt function?
Answer by robertbu · Aug 14, 2013 at 07:12 PM
The look rotation is a direction, not a point in space. To solve your problem:
desiredRotation = Quaternion.LookRotation ( aimPoint - transform.position);
Thank you robertbu! I've been making my eyes bleed combing over that one 47 $$anonymous$$ute tutorial for the past 2 days to see if there was anything I missed. ( sad I know, but I'm really new to this )
This solution does work, though I'm still confused as to why.
Guess I have more reading to do!
aimPoint is a point in 3D space. In the drawing above, it is the green arrow. What you want is a direction relative to the launcher which is the blue line. You generate the blue vector by subtracting the launcher position from the 'aimPoint.' This problem pops up regularly because Transform.LookAt() take a point in 3D space (not a direction). So if you were using LookAt(), 'aimPoint' would have worked.
Ha! That diagram is awesome! Thanks for putting in the effort to explain that, robertbu!
I forgot to mention (but you probably figure it out), that your original code worked because the "launcher" was at the origin, so the red and green arrows were the same.
Oh yeah, I didn't understand at first but I got it once I saw the diagram. ( guess I'm more of a visual learner )
Thanks again!
Your answer
![](https://koobas.hobune.stream/wayback/20220613112217im_/https://answers.unity.com/themes/thub/images/avi.jpg)