Hi people,
I used very basic networking from M2H that one of networked computers is server and the other is client. When they collide with some game object I wanted make the one collided to move to next level (which is different scene file) while the other which didn’t touch the collider still staying at the scene where the game started.
Here is my original post for enquiring about this problem: http://forum.unity3d.com/viewtopic.php?t=53911&highlight=scene
You should store the info on which level the player is in and use that to determine which messages should be displayed and which audio should be played.
A really simple example for the chat:
var currentLevel:int;
var message:String;
var text:String;
function OnGUI(){
GUILayout.BeginVertical();
GUILayout.Label(message);
text = GUILayout.TextField(text, 160);
if(GUILayout.Button("Send")){
if(Network.isServer){
SendMessage(text, currentLevel);
}else{
networkView.RPC("SendMessage", RPCMode.Server, text, currentLevel);
}
}
GUILayout.EndVertical();
}
@RPC
function SetMessage(newMessage:String, level:int){
if(currentLevel == level){
message = newMessage;
}
}
@RPC
function SendMessage(message:String, level:int){
networkView.RPC("SetMessage", RPCMode.Others, message, level);
SetMessage(message, level);
}
@RPC
function SetLevel(level:int){
currentLevel = level;
}
The SetLevel function should be called remotely only for the player who is changing level. I can’t guarantee this script works because I can’t test it right now, but it should give you some ideas.
You need to keep track of which level the player is on. Add a variable to your chat script for it, it can be anything really, a string with the level name, an int with the level number or something like that. When the player moves to another level update the variable accordingly. You also could give the variable to your player script and just read it from there. When you send a message you need to pass that variable on as a parameter along with the actual message. Then on the receiving end check if the received level identifier is the same as the one on the receiver and display the message only if it is.
You can do this with the following changes to your script:
To the beginning of the script add the variable for keeping track of which level the player is on(I’m using a string here, use an int if you want to):
var currentLevel:String;
In the Update function change the following lines:
Thanks for a quick response for such a long question!
Now i’m kinda getting idea but once I implemented the script above i get:
Maybe it’s because RPC thing itself can’t get more than one parameter? I tried making array containing these two strings and let AddChatEntry to accept array as parameter, but there was no use
Oh! sorry boys, I actually made new script to apply the code written above and added to the object. Although I unchecked the old script, it was still functioning somehow.
Although Limit Area of chatting range is stil not successful.
Currently I have set this script onto the plane of level… (where my characters stand on)
var ChatArea: ChatWindow;
function OnMouseDown()
{
GameObject.Find("EnterPad").collider.isTrigger = false;
}
function OnCollisionEnter()
{
ChatArea.currentLevel = "Lobby";
}
and Different String when colliding with plane on other level.
But this doesn’t work because It changes currentLevel value of client too! so even if I move to different level, server and client users can still talk to each other.