- Home /
Optimizing Constrained Turret Aiming
Hey all,
I have a constrained turret composed of a yawing base and a pitching barrel that I'm trying to optimize. I use several turrets on each of my ships, and this tends to add up in performance quite quickly. With 2-8 turrets on each ship, I can generally get to 50 ships (~200 turrets) before I start seeing significant slowdown (traced to the turret code via process of elimination). I've been over the math quite a bit in my head - was wondering if anybody had any ideas for how to streamline this further. I only have Unity Free so the profiler isn't available for more specifics.
The following code is called on FixedUpdate:
void AimTurret()
{
Vector3 aimLocal = Vector3.zero;
if (turretTrack)
{
// get aim position and shift to local coords
aimLocal = turretRoot.InverseTransformPoint( turretTrack.position);
}
AimYawBone(aimLocal);
if (turretTrack)
{
// get aim position and shift to local coords
aimLocal = turretBoneYaw.InverseTransformPoint( turretTrack.position);
}
AimPitchBone(aimLocal);
}
void AimYawBone(Vector3 aim)
{
Quaternion rotGoal = Quaternion.LookRotation(aim);
Quaternion curRot = Quaternion.RotateTowards(turretBoneYaw.localRotation,rotGoal,Time.deltaTime*turretYawSpeed);
Quaternion comparer = turretRoot.localRotation;
comparer.eulerAngles = new Vector3(turretRoot.localRotation.eulerAngles.x,turretRoot.localRotation.eulerAngles.y - turretRoot.localRotation.eulerAngles.y,0.0f);
if (Quaternion.Angle(comparer,curRot) > turretYawRange)
{
} else
{
turretBoneYaw.localRotation = curRot;
}
turretBoneYaw.localRotation= Quaternion.Euler(0.0f,turretBoneYaw.localRotation.eulerAngles.y,0.0f);
}
void AimPitchBone(Vector3 aim)
{
Quaternion rotGoal = Quaternion.LookRotation(aim);
Quaternion curRot = Quaternion.RotateTowards(turretBonePitch.localRotation,rotGoal,Time.deltaTime*turretPitchSpeed);
Quaternion comparer = turretBonePitch.localRotation;
comparer.eulerAngles = new Vector3(initialTurretPitch,turretBonePitch.localRotation.eulerAngles.y, turretBonePitch.localRotation.eulerAngles.z);
if (Quaternion.Angle(comparer,curRot) > turretPitchRange)
{
} else
{
turretBonePitch.localRotation = curRot;
}
turretBonePitch.localRotation= Quaternion.Euler(turretBonePitch.localRotation.eulerAngles.x,0.0f,0.0f);
}
As a note, the relevant section of the hierarchy of the turret works like this:
TurretRoot - TurretYawBone -- Turret PitchBone
It's difficult to tell which part of this is causing the most slowdown. I think that it's probably to some extent the angle comparers, but I can't really work out a better way to do it, my quaternion understanding is poor.
Any ideas are greatly appreciated.
dude, all you're doing here is setting the turret so it is pointing at some position .. is that correct?
yourTurret.eulerAngles = Vec3( 0, 27.35, 0);
is that correct ??
Indeed .. can you just use lookAt ?
(To save you the one line of code to work out what the angle, say "27.35" is ?)
{if the target is not at the same height as all your turrets, just make a "level target" marker (marker == empty gameObject, just a transform) that's at the same position as the target but always floats at the turret height.}
The issue is constraining the turret after the LookAt. Using LookAt and then testing localEulerAngles to see if they are exceeded and clamping the turret produces abnormal results - flickering and jerky rotation.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094311im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Are .gameObject and .transform both using GetComponent() in the background? 1 Answer
Add 2d rigidbodies to moving objects to increase performance or not? 1 Answer
Increase Unity2D editor performance 0 Answers
(2 hidden skinned meshes) or (1 extra draw call) for character props? 2 Answers
Simple Animation showing as ~6.3 in Xcode console profiler 0 Answers