Multiplayer in Laser Defender

Hello all! Just wanted to share a quick screenshot of what I’ve been working on. Most bugs are ironed out aside from the “NullReferenceException: Object reference not set to an instance of an object
ScoreDisplay.Update () (at Assets/Scripts/ScoreDisplay.cs:22)” which leads to “scoreText.text = gameSession.GetScore(tag).ToString();” under the update method in ScoreDisplay. It seems to be more of a performance issue than anything else, though since the game is so small scale it hasn’t affected anything yet. I found a way to take that bug out with the intention to increase performance, but I ended up with a much worse bug that actually stopped showing the total score on the game over screen. So I went with the lesser evil and stuck with the one the player wouldn’t notice. You may notice in the code above it says tag. To implement the multiplayer function without making a whole other Player2 class I just used tags and returned the tags in order to know which score (and health) text to update. If anyone is interested I can share more of the code. Here’s what it looks like. I’ll share a link to play the game after I put in a couple more features (such as switching between 1 and 2 players) and work on level design.


-Addy

1 Like

I would love to see more code! I’m more so a designer in the course so I might have some trouble implementing it but i’m willing to give it a shot!

I would also be very much interested in the code. :slight_smile:

Wow, sorry for the immensely late reply @Jedediah_Leavell! Hope this still helps you and @Richard_Beleznay. I’m still working with figuring out GitHub when it comes to Unity, so please make due with the very unorganized repository: https://github.com/Addikins/UdemyProjects/commit/ae5e2b9cfd90c352a07ee649e289c41a56420cde
Just follow the link to my GitHub, it’ll bring you to all the recent commits which are the scripts for the entire Lazer Defender game. Also, I have a working standalone build but the UI was giving me issues with the Web build.

P.S. I’m still active around here, however I’ve been focusing on my frontend dev skills recently.

1 Like

I can’t complain. You replied to me in 1 hour :wink:
Thank you very much for sharing your code! It is a huge help for us.

I took a quick look with my beginner skills and I noticed in the player.cs at the Move part that you used

  • Input.GetAxis(“Vertical”) and horizontal for player 1 and
  • Input.GetAxis(“Vertical2”) and horizontal2 for player 2.
    Because of my limited coding experience I’d like to ask: How exactly this works? w-a-s-d moves player 1 around and the arrow keys move the player 2? Does unity understand the difference between “vertical” and “vertical2” automatically or you’ve set up the key bindings for that somewhere?

Thank you for your help!

Great question! It is a keybinding that I setup. I changed a little bit around in Unity’s input settings, it’s really easy. We messed with this earlier in the course just a bit. I’ll get a screenshot once I’m at my desk :slight_smile:

Alright, here they are. Just go to Edit > Project Settings > Input to get here. Sorry for the misleading “Fire 1.5”, I think that was just sloppy work on my side because I was using Fire 2 for something IIRC. Let me know if it works for you!

Oh and to answer your questions, It’s set up for a game controller to work as the second player, however, if you go into those project settings you can map the controls to anything you want. So yes, you can easily have it read wasd+space bar for player one and up/down/left/right+num0(or whatever). Also, the Inputs can be named whatever you want, so you could say “Player 1 Vertical Movement” as the name. I put descriptions just to help out a little. Just make sure they match what your code is trying to reference.

Privacy & Terms