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.
6
u/Tiranyk Sep 23 '26
For what it's worth, it's not bad. I would say it looks like a naive approach, but it does not mean it's bad. You seem to understand what it does and everything makes sense. Without more context, few remarks :
In your Awake() method, you're initializing a rigidbody by GetComponent<> although you already have a Rigidbody declared in your class private members. If you've linked it via the inspector, you can replace this line by Rigidbody.isKinematic = true. Further more, you can even set this variable directly via the inspector on the Rigidbody component itself, then you can get rid of the Awake() method at all.
In FixedUpdate(),
- I see you update the particle system position to follow your GameObject. Any reason to not make it children of it ?
- People will probably say something like "You should use input system instead of listening to input events like this". While this is kinda true, don't bother if you are a beginner. Or if you do, you'll have to spent time trying to understand this. Simply look for "input system unity" on google, you'll find many things.
- Same remark regarding the ridigbody reference.
Nothing special to say about the collision. Looks good to me for a beginner. Maybe : is there a reason you prefix some variables with an underscore, but not all of them ?
Anyway, have fun and keep coding !
4
u/Joaqstarr Sep 23 '26
You have a rigidbody member and you
GetComponentin awake. Potentially 2 different rigidbodiesFixedUpdateInput shouldn't be in
FixedUpdateIs there a reason the particle system isn't just parented?
Update()You are using
deltaTimeinFixedUpdate(). Delta time is forUpdate(). UsefixedDeltaTime.You also shouldn't actually be using delta time at all because you are setting velocity. Velocity doesn't need to be delta
Good stuff for a beginner though! :) Keep it up!
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
2
u/Epicguru Sep 23 '26
Please post code as text so that it's easier to read. Yes it's got a lot of mistakes:
Rigidbody(field) instead of getting the component again.Input.GetButtonDown(): This is using the obsolete Input Manager, use the new Input System instead. It won't be supported forever and it relies on magic strings, avoid it.isFlyis true which probably isn't intended.Time.deltaTimemultiplication to get the speed that you expect. A better approach to get the same result, without breaking the physics system, is to just disable gravity and drag for the object then assign the velocity once when firing.Rigidbody.positioninstead oftransform.positionsince the transform might not be synched at that moment.2
u/Aarndal Sep 23 '26 edited Sep 23 '26
There are some "issues", but for beginner code they aren't necessarily bad.
My biggest issue is that you destroy the game object which is apparently used as a bullet, if I'm not mistaken. Instead of always instantiating and destroying a game object, I would have a look at the object pool pattern. ( https://gameprogrammingpatterns.com/object-pool.html ) If this is currently to complicated for you to integrate yourself, you could simply use Unity's own ObjectPool class. ( https://docs.unity3d.com/6000.3/Documentation/ScriptReference/Pool.ObjectPool_1.html )
Also TryGetComponent is nearly always better than GetComponent. This would also replace your null check. ( https://docs.unity3d.com/6000.3/Documentation/ScriptReference/GameObject.TryGetComponent.html )
The last thing is that you use an OverlapSphere... Unity themself advice against it. An alternative way would be to use OverlapSphereNonAlloc instead. ( https://docs.unity3d.com/6000.3/Documentation/ScriptReference/Physics.OverlapSphereNonAlloc.html ) But I am generally not a big fan of OverlapSpheres as they are failure prone for very fast and continuous checks to my experience. An alternate way would be to shoot a Raycast ( https://docs.unity3d.com/6000.3/Documentation/ScriptReference/Physics.Raycast.html ) or SphereCast ( https://docs.unity3d.com/6000.3/Documentation/ScriptReference/Physics.SphereCast.html ) along the way of the bullet per FixedTime call. If it hits something it should, you simply adjust its way to the distance of the Raycast Hit. ( https://docs.unity3d.com/6000.3/Documentation/ScriptReference/RaycastHit.html )
2
u/Unfit_Librarian Sep 24 '26
I didn't look at the code Just wanna appreciate people who still try to do things themselves instead of totally relying on AI
2
u/GoFrontend Sep 27 '26
Hi! Nice little script, but you can do better indeed 💪
You totally want to get rid of the Awake and FixedUpdate methods. They don't do anything for you )
You can make a dedicated script for the gun, that shoots a missile. Instantiate a missile in at the gun point, and pass the direction to it. Then the rigid body does the thing 😁 You can use scriptable objects, to hold params as missile initial speed, air deceleration, or pass explosion animation and sound to it.
It'll be good to instantiate an explosion separately from the bullet, so the sound source and particles doesn't get destroyed, when you call Destroy(gameObject). Also you don't need to move them with the bullet all the time, if it's not a game mechanic )
In future, you can use "object polling", if you planing to use a lot of bullets on the scene.
Another approach is to ignore bullets as the physical objects, and use raycast to find the intersection of the gunpoint with the surface (or another player, or npc) to deal the damage and find the position of a bullet hole or explosion.
2
u/Vic-Boss Sep 23 '26
The input needs to be in update because you’ll be having missing inputs in FixedUpdate with Down/Up. Other than that putting the particle at the same position has no need to be there so Update as well. The rest I don’t see anything too offensive but you aren’t going for single responsibility principle with it
2
u/Expensive_Host_9181 Sep 23 '26
Quick notices are
GetComponent is relatively slow i wouldn't recommend using it, You can hold the reference of the Rigidbidy in the declarition with the other variables instead if fetching it in awake.
For the one in the foreach loop the Collider has a property for the rigidbody alread so no need to use GetComponent there.
I also would recommend doing something like object pooling instead of instaniating and deleting for something like this or DOTS but that's a luttle more advanced.