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
0
Question by Fierce Waffle · Jan 29, 2011 at 01:59 AM · fpscharacternetworkcontrolsother

Networking Script Controls the Opposite Player

I have made two netowrking scripts so far. here is the first.

using UnityEngine; using System.Collections; using System;

public class MPBase : MonoBehaviour {

 public string connectToIp = "127.0.0.1";
 public int connectPort = 25000;
 public bool useNAT = false;
 public string ipaddress = "";
 public string port = "";

 string playerName = "<NAME ME>";

 void OnGUI()
 {
     if (Network.peerType == NetworkPeerType.Disconnected)
     {
         if (GUILayout.Button("Connect"))
         {
             if (playerName != "<NAME ME>")
             {
                 Network.useNat = useNAT;
                 Network.Connect(connectToIp, connectPort);
                 PlayerPrefs.SetString("playerName", playerName);
             }
         }

     if (GUILayout.Button("Start Server"))
     {
         if (playerName != "<NAME ME>")
         {
             Network.useNat = useNAT;
             Network.InitializeServer(32, connectPort);

             foreach (GameObject go in FindObjectsOfType(typeof(GameObject)))
             {
                 go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
             }
             PlayerPrefs.SetString("playerName", playerName);
         }
     }
         playerName = GUILayout.TextField(playerName);
         connectToIp = GUILayout.TextField(connectToIp);
         connectPort = Convert.ToInt32(GUILayout.TextField(connectPort.ToString()));
     }
     else
     {
         if (Network.peerType == NetworkPeerType.Connecting) GUILayout.Label("Connect Status : Connecting");
         else if (Network.peerType == NetworkPeerType.Client)
         {
             GUILayout.Label("Connection Status : Client!");
             GUILayout.Label("Ping to Server: " + Network.GetAveragePing(Network.connections[0]));
         }

         else if (Network.peerType == NetworkPeerType.Server)
         {
             GUILayout.Label("Connection Status : Server!");
             GUILayout.Label("Connections: " + Network.connections.Length);
             if (Network.connections.Length >= 1)
                 GUILayout.Label("Ping to Server: " + Network.GetAveragePing(Network.connections[0]));
         }

         if (GUILayout.Button("Disconnect"))
             Network.Disconnect(200);

         ipaddress = Network.player.ipAddress;
         port = Network.player.port.ToString();
         GUILayout.Label("IP Address: " + ipaddress + ":" + port);
     }
 }

 void OnConnectedToServer()
 {
     foreach (GameObject go in FindObjectsOfType(typeof(GameObject)))
     {
         go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
     }
 }

}

This was written in C#. Now here is the next, also written in C#

using UnityEngine; using System.Collections;

public class SpawnScript : MonoBehaviour { public Transform player;

 void OnServerInitialized()
 {
     SpawnPlayer();
 }

 void OnConnectedToServer()
 {
     SpawnPlayer();
 }

 void SpawnPlayer()
 {
     Network.Instantiate(player, transform.position, transform.rotation, 0);
 }

 void OnPlayerDisconnected(NetworkPlayer player)
 {
     Network.RemoveRPCs(player);
     Network.DestroyPlayerObjects(player);
 }

 void OnDisconnectedFromServer(NetworkDisconnection info)
 {
     Network.RemoveRPCs(Network.player);
     Network.DestroyPlayerObjects(Network.player);
     Application.LoadLevel(Application.loadedLevel);
 }

}

And I also added this code to my MouseLook script and FPS Script (JavaScript)

function Start() { if(!networkView.isMine) { enabled = false; } }

function Whatever() { if(networkView.isMine){ //do whatever } }

The only problem is that when I connect with two clients, Player1 controls player 2's character and Player2 controls Player1's. Please leave a response or help me one on one by contacting me at fiercemodz@gmail.com

Comment
Add comment · Show 1
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 marf · Jul 25, 2011 at 03:56 PM 0
Share

I've the same problem, can you tell me how to fix it?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by seth · Jan 29, 2011 at 12:42 PM

Hey. Check if setting "observed" field in your "network view" component to none (instead of a supposed Transform:GameObject that might be there, would help. there's a possibility that you want to use the networkView just for RPC calls and, by mistake, you're updating everyone's position this way.

At least that's what happened to me before.

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 marf · Jul 25, 2011 at 03:56 PM 0
Share

So, i need to change the observed to none?

avatar image
0

Answer by roamcel · Aug 05, 2011 at 10:33 AM

Your need is documented in the official netwokring example. What you basically need is to get the 'others' controller scripts and disable them in each client, once they spawn. However this is only a small part of the whole solution, which is pretty complicated. link text

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 suyujin · Dec 29, 2011 at 04:28 AM

Hey, I'm not sure if you have solved this yet or not, but I had a similar problem. If yours is what mine was, all you'll need is to create a .js file and put it on the camera. This is all you need in that file. function Awake() {

 if(networkView.isMine){
     camera.enabled = true;
 }else{
     camera.enabled = false;
 }

}

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to fix the fps controllers, that has rigid body and network 0 Answers

how i can combinemultiple mesh colliderrs in to 1 big mesh 0 Answers

FPS Network 0 Answers

How to Network Sound? 1 Answer

How does the client command the server in Javascript? 1 Answer


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