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 DeadKenny · Sep 10, 2013 at 08:01 PM · c#optimizationlagefficiencyturrets

Is this code inefficient? It is causing Lag.

I am sure this code is bad, can you guys tell me what it could be?

I am seeing a massive drop in fps when I start it up but it goes back up to about 40-50 but still much lower than when its off, which is 80-90 even 100+.

I might be going crazy though you never know.

The things I am sure are not causing it in the code.

The cameras are set to enable and disable once in and so is the entire players object once I get in the vehicle with the turret. So its not an extra camera issue I made double sure only one is enabled when using the vehicle and turret.

Here is the code. It is a turret on a vehicle with a weapon.

 using UnityEngine;
 using System.Collections;
 
 public class TurretController : MonoBehaviour {
     
     //Class to draw range and stuff from.
 //    private TurretClass turretClass;
     
     
     
     public Transform barrel;
     public Transform firePoint;
     
     public GameObject projectileMain;
     public GameObject muzzleEffect;
 
     public float shotForce = 0;
     public double shotDelay = 0; 
     private double endDelay = 0;
     
     private int shotCount = 0;
     public float range = 1000;    
     
     
     
     public enum RotationAxis {MouseX = 1 , MouseY = 2}
         
     public RotationAxis turretRotXY = RotationAxis.MouseX | RotationAxis.MouseY;
     public RotationAxis barrelRotXY = RotationAxis.MouseX | RotationAxis.MouseY;
     
     
     
     public float turretSensitivityX = 400f;
     public float barrelSensitivityY = 400f;
     
     
     
     public float turretMinimumX = -360;
     public float turretMaximumX =  360;    
     private float turretRotationX = 0f;
     
     public float barrelMinimumY = -360;
     public float barrelMaximumY =  360;    
     private float barrelRotationY = 0f;
     
     
     public Quaternion OrigRotation;
 
     
     
     
         
     public void Awake(){
         
         
         endDelay = Time.time;
         
         
         
         
     }
     
     public bool Recharged()
     {
         
         return (Time.time > endDelay);
         
         
         
     }
     
     
     
     
     
     // Use this for initialization
     void Start () {
         
 //        shotForce = _turretClass.GunRange;
         
     
         OrigRotation = transform.localRotation;
         
         
     }
     
     // Update is called once per frame
     void Update () {
     
         Turret();
         
         Barrel();
         
         
         Shoot();
         
         
     }
 
     void Turret(){
         
         if(turretRotXY == RotationAxis.MouseX) {
             
             turretRotationX += Input.GetAxis("Mouse X") * turretSensitivityX * Time.deltaTime;
             
             turretRotationX = ClampAngle(turretRotationX, turretMinimumX, turretMaximumX);
             Quaternion XQuaternion = Quaternion.AngleAxis(turretRotationX, Vector3.forward);
             transform.localRotation = OrigRotation * XQuaternion;
     }
         
         
         
         
     }
     
     void Barrel(){
         
         
         if(barrelRotXY == RotationAxis.MouseY) {
             
             barrelRotationY += Input.GetAxis("Mouse Y") * barrelSensitivityY * Time.deltaTime;
             
             barrelRotationY = ClampAngle(barrelRotationY, barrelMinimumY , barrelMaximumY);
             Quaternion YQuaternion = Quaternion.AngleAxis(barrelRotationY, Vector3.left);
             barrel.localRotation = OrigRotation * YQuaternion;
         
         
         
         }
     }
     
     
     public static float ClampAngle (float Angle, float Min, float Max) {
         
         
         if (Angle <-360) {
             
             Angle += 360;
         }
         if(Angle > 360) {
             
             Angle -= 360;
         }
         
             return Mathf.Clamp (Angle, Min, Max);
    }
     
     
     
     
     
     void Shoot(){
         
         
             if(Input.GetKeyDown(KeyCode.Mouse0) &&  Recharged()){
             
                 Instantiate(muzzleEffect, firePoint.position, firePoint.rotation);
             
               Instantiate(projectileMain, firePoint.position, firePoint.rotation);
             
             endDelay = Time.time + shotDelay;
             shotCount++;
             
         }
         
                  
     }
     
          
 }


I have a good feeling it has got to do with the rotation of the turret not the barrel.

So yeah, you guys see something bad here?

Comment
Add comment · Show 2
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 whydoidoit · Sep 10, 2013 at 08:08 PM 0
Share

That code is fine, nothing slow in that. It must be something else.

Also that ClampAngle function isn't actually going to work right with certain sets of angles, but it might be ok with the small deltas you are making and the choices of angle.

avatar image DeadKenny · Sep 10, 2013 at 08:19 PM 0
Share

Delta what now? The turret is working perfect, but its causing lag...or maybe its the initializing of the entire car that slows it down. Because 2 cameras have to go on and then the wheels...$$anonymous$$aybe its the wheels... hmmm. Nah can't be because I have a tank too and it causes the same kinda lag when the turret is activated.

If I deactivate the turret during play, the frame rate noticeably jumps up... and it drops to 4, 6 even 12 fps when I first get in the car with turret on. after a bit of turning and looking around it goes back to 20 fps or so less than normal around 60-50 fps.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sisso · Sep 10, 2013 at 08:18 PM

I didn't see any clear problem. The only real possible problem is if method Shoot has a bug that it instantiate both objects each frame.

I don't thinkg that this is script is the problem, but it could be activating the problem. For example: I am empty scene only with static objects, no physic must be computed. But if you put a single ball with rigidbody you will loss many fps because the physic must be computed, but you can add 100 balls without lose any fps.

Try to dig better where you are losing by commenting some code in the script, like changes in transformation.

Comment
Add comment · Show 1 · 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
avatar image DeadKenny · Sep 10, 2013 at 08:22 PM 0
Share

Hmmmmmmm...... The hell could it be?

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

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

Related Questions

One big script or lots of small ones? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Public, private and getting stuff efficiency question. 2 Answers

How do you efficiently target many instantiated prefabs for tweening? 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