Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by iamkirkos · Dec 03, 2013 at 01:13 AM · 2dmultiplayergameoffline2 player

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baalhug · Dec 03, 2013 at 01:19 AM 0
Share

Of course it is possible. What's the problem on that?

avatar image Baalhug · Dec 03, 2013 at 01:21 AM 1
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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:

Example Project

 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);
 
 
     }
 }



two players example.zip (149.9 kB)
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iamkirkos · Dec 03, 2013 at 02:13 AM 2
Share

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!

avatar image clunk47 · Dec 03, 2013 at 02:19 AM 2
Share

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.

avatar image Rs31412 · Mar 11, 2014 at 04:30 AM 0
Share

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.

avatar image
0

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)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joe-Censored · Apr 10, 2017 at 08:07 PM 1
Share

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.

avatar image
0

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) alt text 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.


screen-shot-2017-07-06-at-94959-pm.png (22.5 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[2D] How to make player face camera when rotating? 1 Answer

How to search for multiplayer games 0 Answers

2d Lighting - Weird light triangles 0 Answers

Mouse cursor position shared beetwen different builds. 3 Answers

Photon - How can I get a list of ALL players in EVERY room? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges