- Home /
How can I split up player control logic?
I'm trying to understand how best to split up my c# scripts to control my player's 3D character in my game.
The game is mouse click controlled, so if the player clicks on empty ground the character runs to that spot. If the player clicks on an enemy, the character will run to within their weapon's range and shoot the enemy (or that's the goal at least).
I've started by following Unity's reworking of their Survival Shooter series, where they convert it to a 'click to move' game (here).
So far I have the following scripts/classes:
PlayerMovement
PlayerShooting
..attached to the character. But now, I want to create a separate input class with a 'MouseClick' event to separate out input control. I also hope this will better enable me to add other player characters to create a team, who will move and shoot alongside each other (like Cannon Fodder).
However, I cant see how to break it down logically in to:
input
movement
targeting
shooting
animation
My confusion stems from the complexity of all the parts all referencing each other at every turn. ie raycast hit points, isRunning, isEnemyClicked, onMouseClick and moving to point or within range of enemy etc.
So, I'm asking: what is the best way to break up these components and getting them to communicate?
Answer by Badgerfish · Oct 15, 2015 at 06:12 PM
RaycastHit returns the a lot of info about what it hit, which means if you hit (clicked on) an 'enemy', it will return that enemy's information.
Using hit.transform.gameObject, you can look at the game object's tag, and if it's tagged as an 'Enemy' you can then set the player's action to be something different than just move.
If the tag is, for example, 'Floor' you can set it so the player only does his basic movement.
If the tag is, something like an intractable object (like a chest or 'pickup' item) then the character can do something else, like an open chest animation or something.
Thanks for this @Badgerfish. I am already doing this, what I'm struggling with is visualising how to split up the logic between PlayerInput, Player$$anonymous$$ovement and PlayerShooting.