Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Chris42 · Mar 12, 2013 at 11:09 PM · performanceoptimizationturrets

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Mar 13, 2013 at 10:07 AM 0
Share

you have a "BOATLOAD" of turrets ... haw haw ! :)

avatar image Fattie · Mar 13, 2013 at 10:14 AM 0
Share

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.}

avatar image Chris42 · Mar 13, 2013 at 05:42 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges