I am a beginner in regards to Unity and I’ve set myself the task of creating a basic 2 Player, head to head style game. The game would draw from Super Smash brothers brawl in the sense of gameplay.
Is it possible to create such a game? Would anyone have pointers to any guides or tutorials on how to create an offline multiplayer.
When I talk about offline multiplayer I mean a game where two players are controlled on the same keyboard. For example, Player 1 would be controlled by w,a,s,d and Player 2 would be controlled by the arrow keys.
I hope I explained that clearly and you understand what I’m aiming for. Thanks for your attention
Well this is kind of a broad question, but the answer, “Of course”. You simply create a controller script determines what keys are used for each character. It’s pretty much the same principal as having two guns in a fps, and firing the left gun with left mouse click, and right gun with right click… You could use Instance IDs or just a Generic List of sorts to determine the ID of each instantiated character (prefab). Try something like this. Place two cubes in your scene and tag them both “Player”. In my scene, I have this script attached to an empty gameObject called “scripts”. I have attached an example project as well. C# Example:
[18809-two+players+example.zip|18809]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Example : MonoBehaviour
{
public GameObject playerPrefab;
List<GameObject> players = new List<GameObject>();
float speed = 3;
void Update()
{
foreach(GameObject player in GameObject.FindObjectsOfType (typeof(GameObject)))
{
if(player.tag == "Player" && !players.Contains(player))
players.Add (player);
}
if(Input.GetKey(KeyCode.A))
players[0].transform.Translate (Vector3.left * speed * Time.deltaTime);
if(Input.GetKey(KeyCode.D))
players[0].transform.Translate(Vector3.right * speed * Time.deltaTime);
if(Input.GetKey (KeyCode.W))
players[0].transform.Translate(Vector3.up * speed * Time.deltaTime);
if(Input.GetKey (KeyCode.S))
players[0].transform.Translate (Vector3.down * speed * Time.deltaTime);
if(Input.GetKey(KeyCode.LeftArrow))
players[1].transform.Translate (Vector3.left * speed * Time.deltaTime);
if(Input.GetKey(KeyCode.RightArrow))
players[1].transform.Translate(Vector3.right * speed * Time.deltaTime);
if(Input.GetKey (KeyCode.UpArrow))
players[1].transform.Translate(Vector3.up * speed * Time.deltaTime);
if(Input.GetKey (KeyCode.DownArrow))
players[1].transform.Translate (Vector3.down * speed * Time.deltaTime);
}
}
Can you please specify exactly how to do this, what should I name the script, can I put it on the objects I want to controller?
All I want is a step by step instruction.
(I am really new to unity)
I am aware that this question has already been answered, however that way isn’t exactly very efficient or adaptable.
The way I did this was I accessed the input manager by going EDIT > PROJECT SETTINGS > INPUT. I then created a second control scheme and then adapted the default one so that they both had separate controls and names (as seen below)

After that I duplicated my base movement script and then altered the code of the new script. For instance, instead of accessing “Jump” it would access “Jump1”
Here is my original script:
using UnityEngine;
using System.Collections;
public class Player1Movement : MonoBehaviour {
//Variables
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
// is the controller on the ground?
if (controller.isGrounded) {
//Feed moveDirection with input.
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection *= speed;
//Jumping
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
}
And here is my new one:
using UnityEngine;
using System.Collections;
public class Player2Movement : MonoBehaviour {
//Variables
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
// is the controller on the ground?
if (controller.isGrounded) {
//Feed moveDirection with input.
moveDirection = new Vector3(Input.GetAxis("Horizontal1"), 0, Input.GetAxis("Vertical1"));
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection *= speed;
//Jumping
if (Input.GetButton("Jump1"))
moveDirection.y = jumpSpeed;
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
}
As you can see, I changed the control layout that was being detected. It’s through this that you can create 2 separate players and then apply one script on P1 and the other to P2.
I used an actual controller for my player two so everything became pretty easy and comfortable and if you have the option, I suggest you do the same if you can.
Hey @clunk47 @iamkirkos check out this tutorial for very easy scripts.