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 DMCH · May 29, 2013 at 12:14 AM · c#optimizationbullettargeting

Top Down 2d (PC): Targeting code optimisation

Hello,

Thanks for reading. I'm fairly new to optimisation, and I've recently been doing some stress testing on the game I'm working on, which is a 2d top down shooter for PC. There could potentially be up to 7 turrets and maybe more than 40 enemies in game at once. I'm getting drops to around 40 fps in the editor mode with 4 turrets. I'm aware 40 is still quite high, but my comp, although a few years old is fairly decent, and I'd like the game to run on lower end hardware.

I think one of the problem areas may be the code turrets use to target enemies. The code for targeting (which I've posted below) is called once per update while an enemy is in the the turret's range (trigger) collider. Can anyone advise me whether calling this up to 7 times per update is chewing up CPU time? Is there any way this could be optimised, or called less, without detracting from the accuracy?

 // Check that the target is still in range 
 float targetDistance = Vector3.Distance(target.transform.position, transform.position);
                 
 if(targetDistance <= (attackRadius + 1)) 
 {
     // Get target's transform.position
     targetPosition = target.transform.position;
                     
     // Rotate to Target
     RotateTurret();
 }

The RotateTurret() method

 // Get current velocity of target
 Vector3 targetMovePerSec = target.rigidbody.velocity; // since you physics to move
 targetMovePerSec.y=0; 
         
 // Predict the future position of the target
 Vector3 estHitPos = targetPosition + targetMovePerSec * secsFlightTime;    
         
 // Get the rotation needed to hit the enemy
 Vector3 aimDirection = estHitPos - bulletSpawn0.transform.position;
         
 // Rotate turret to face that position
 Quaternion neededRotation = Quaternion.LookRotation(aimDirection);
 transform.rotation = Quaternion.Slerp(transform.rotation, neededRotation, rotationSpeed * Time.deltaTime);

I'm considering the idea of making the bullets fired by turrets homing, so the turret will only have to be facing roughly in the direction of the enemy, but this will require more calculations in the bullet script. Can anyone offer me some advice on a good way to deal with this? Thanks!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Adam_Benko · Dec 23, 2018 at 04:39 PM

Have you solved this problem ? I am creating a space shooting top down game. Could you share this script with me ? It would help me a lot. Thanks bro. @DMCH

Comment
Add comment · Share
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

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

14 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 avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flood filling algorithm doesn't work as expected - it creates gaps between pixels 0 Answers

Make my gun model fire bullets 1 Answer

How do I fire a bullet where my player is facing? (C# 2D) 2 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