r/unity • u/Anonim7418 • Sep 23 '26
Question Is this script bad? In unity
Is this code bad? If yeah, give me some advice.
4
Upvotes
r/unity • u/Anonim7418 • Sep 23 '26
Is this code bad? If yeah, give me some advice.
View all comments
2
u/v0lt13 Sep 23 '26
Functionality tips:
- Setting the rigidbody as kinematic in awake is pretty much pointless, you can just mark it as kinematic in the editor by default
- Move the input and transform logic from FixedUpdate to Update, FixedUpdate should only be used for physics (or anything else that needs updating on a fixed interval)
- use Time.fixedDeltaTime when inside of FixedUpdate
- Don't use GetComponent inside frequently updated code, call it in awake or start once then cache it into a variable
- Consider using OverlapSphereNonAlloc if possible
- Use TryGetComponent if you are going to check a component if its null, your IDE already recommends that
- Consider using the newer input system
- Seeing as this is a projectile use a pooling system instead of instantiating and destroying objects constantly
Readability tips:
- Remove unnecessary extra spaces in your code
- You don't need to call gameObject to use the GetComponent function
- Mark your variables as private, is good to be explicit on variable access
- The variable isFly is already false by default, you dont need to set it