Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by 0ctothorp · Jul 14, 2016 at 07:20 PM · c#2dmultiplayermultiplayer-networking

HLAPI simple 2 player game

So I'm making a simple 2 player game following this guide: https://docs.unity3d.com/Manual/UNetSetup.html but instead of 3d shooting game I'm making 2d top-down game.

One of players is on the bottom of the screen and shoots up and the second player is on top of the screen and should shoot down. However I can't figure out how do I make the second player shoot down. I tried to check if player is not a server and apply negative speed to its bullets but it doesn't seem to work.

There's a picture of what I'm working on for better understanding: alt text

Here is my code:

Script attached to player - PlayerScript.cs:

 using UnityEngine;
 using UnityEngine.Networking;
 
 public class PlayerScript : NetworkBehaviour {
     public float speed = 5.0f;
     public GameObject projectilePrefab;
     public int rpm = 120;
 
     private float lastTimeFired = 0;
     private float timeBtwnShots;
 
     void Start () {
         timeBtwnShots = 60.0f / rpm;
     }
 
     public override void OnStartLocalPlayer() {
         GetComponent<SpriteRenderer>().color = Color.black;
         if (isServer) transform.Translate(new Vector2(0, -3));
         else {
             transform.Translate(new Vector2(0, 3));
             transform.RotateAround(transform.position, Vector3.back, 180);
         }
     }
 
     void Update () {
         if (!isLocalPlayer) return;
 
         if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
             gameObject.transform.Translate(new Vector2(-speed * Time.deltaTime, 0));
         else if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
             gameObject.transform.Translate(new Vector2(speed * Time.deltaTime, 0));
 
         if(Input.GetKey(KeyCode.Space)) {
             CmdFire();
         }
     }
 
     [Command]
     void CmdFire() {
         if (Time.time - lastTimeFired > timeBtwnShots) {
             GameObject g = Instantiate(projectilePrefab, transform.position + new Vector3(0, 0.5f, 0), Quaternion.identity) as GameObject;
             g.transform.parent = gameObject.transform;
             lastTimeFired = Time.time;
             NetworkServer.Spawn(g);
             Destroy(g, 3.0f);
         }
     }
 }
 

Script attached to fired projectiles - ProjectileScript.cs:

 using UnityEngine;
 
 public class ProjectileScript : MonoBehaviour {
     public float speed = 5.0f;
     
     void Start() {
         if (transform.parent.gameObject.GetComponent<PlayerScript>().isServer == false)
             speed *= -1;
     }
 
     void Update () {
         transform.Translate(new Vector2(0, 1) * speed * Time.deltaTime);
     }
 }
 

I want client to shoot down. I thought this line in ProjectileScript.cs would make it: if (transform.parent.gameObject.GetComponent<PlayerScript>().isServer == false) speed *= -1;, but it doesn't work.

BTW I've noticed a strange thing with g.transform.parent = gameObject.transform; line: sometimes it just doesn't work... Projectiles spawn without parent in objects hierarchy.

Comment
Add comment
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

1 Reply

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

Answer by 0ctothorp · Jul 21, 2016 at 02:52 PM

Ok, so After reading on the Unity manual I noticed what I was doing wrong. Now my project looks like that:

  • My NetworkManager game object has custom Network Manager Script:

      using UnityEngine;
      using UnityEngine.Networking;
     
       public class CustomNetworkMgr : NetworkManager {
         public Vector3 positionUpper;
         public Vector3 positionLower;
     
         public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
             Vector3 position;
             Quaternion rotation;
             if (numPlayers == 0) {
                 position = positionLower;
                 rotation = Quaternion.identity;
             } else {
                 position = positionUpper;
                 rotation = Quaternion.Euler(0, 0, 180);
             }
             GameObject player = (GameObject)Instantiate(playerPrefab, position, rotation);
             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
         }
     }
    
    

    As you can see I'm assigning players positions in OnServerAddPlayer.

  • PlayerScript changes include:

    • adding [SyncVar] private bool shootDown = false;

    • changing OnStartLocalPlayer to just:

       public override void OnStartLocalPlayer() {
           GetComponent<SpriteRenderer>().color = Color.black;
       }
      
      
    • In CmdFire after instantiating player object: if(shootDown) g.GetComponent<ProjectileScript>().speed *= -1;

  • In ProjectileScript I made speed variable a [SyncVar]

That's it! Now players shoot projectiles as intended. Gif showing how it's working now

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

221 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 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 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 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 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 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 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 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 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

Comparing scores on multiplayer 0 Answers

Match making in Photon 0 Answers

How to find local player gameobject? c# 3 Answers

[SOLVED] PhotonNetwork.Instantiate cannot spawn players 0 Answers

Host looks laggy to the client, but not vice-versa. 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