How can I have two classes in the same script, but choose not to use one on a certain object?

Basically I have the main player which is a ship. The player uses a player script that gives it all the movement and variables through the inspector.

On top of the player (in-game, but is a child of Player in the editor) is a cannon, and it’s only purpose is to point towards the mouse pointer.

I didn’t want to make an entire script for it just so it could point towards the mouse, so I tried making a new class for it in the Player script, but I realized that I had no way to differentiate the cannon from the player, thus it inherited all of the players code and it required me to reference all of the serialized variables in the inspector.

Is there any way to code a simple point-to-mouse method for my cannon without making an entirely new script? Thanks!

The approach of “without making a new script” isn’t always a very productive one in game dev and programming.

Just make a separate component for the cannon’s mouse look. This will be less complicated than trying to mash together responsibility.

1 Like

I think I know what you mean? Just make a reference to the cannon’s transform and rotate it through the player script?

That’s literally the opposite of what I said. I’m effectively saying “make a new script”.

If the player and cannon both require mouse look, move all that into a separate component to drive both.

1 Like

I guess I forgot to clarify. The player doesn’t need to point towards the mouse, only the bullets do, and those already have a separate script. I think I found a solution though.