- Home /
Is it possible to create a 2 Player Offline game
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
If you use Input.Get$$anonymous$$eyDown() function you can do it. You only must set which keys will animate player 1 and which ones will animate player 2, all of them using the same function.
Answer by clunk47 · Dec 03, 2013 at 01:34 AM
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:
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);
}
}
That's create! I'm extremely new to Unity and any sort of coding. (I began yesterday) I'm just trying to break down the code and understand it. I'm a little confused though. How does the code choose between Players[0] and players[1]. Is that defined in the game object somewhere? Sorry for the continuation of questions. Is there any forums or communities that would be happy to talk about this stuff with me? Thanks!
0 and 1 are the indexes in the list. players[0] is the 1st gameObject tagged player that the script finds, players[1] is the second player object found.
Sorry to reopen this question, but I've been searching all over the place to try to get this to work. Will this code work if the objects we want to add are GameObjects holding multiple other GameObjects? Say, for example, you want your player to have another Collider or another object to reference a transform location or particle effects? I've tried using the couple of lines for creating a list to make a list of my players (holding several other game objects that refer to things to pick up a ball at a specific location, fire a gun, and have particle effects), but it won't go. It keeps saying I need a rigidbody on every single item listed under my main Player or I get an ArgumentOutOfRangeException.
Answer by Ender_Gamer18 · Apr 10, 2017 at 06:54 PM
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)
If you're asking for someone to basically create you a game development tutorial, you really should be going through the many existing ones online and then co$$anonymous$$g back to more advanced topics like this. Once you can get yourself to the point you can move a single object around then the answer here will be obvious for you to implement.
Answer by SoloTree · Jul 06, 2017 at 02:03 PM
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.
Answer by PRABHBIR123 · Feb 12, 2019 at 02:52 AM
Hey @clunk47 @iamkirkos check out this tutorial for very easy scripts. https://youtu.be/UPQ--4dtmEM