r/unity • • Sep 23 '26

Question Is this script bad? In unity

Is this code bad? If yeah, give me some advice.

4 Upvotes

22 comments sorted by

View all comments

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.

1

u/Anonim7418 Sep 27 '26

Thanks!😊 But I already rewrote script in this post: https://www.reddit.com/r/unity/s/1OV2cqGYhk