Hello,
I am trying to create a small game, to start with, in Unity3D. This is supposed to be a basic game that works over a network. So far, I have my network working, and I had it where one player could see another player moving, and vice versa.
Here are my problems:
- I don’t know how to add gravity to my cube (player). Using Rigidbody has not worked, because it’s caused my cube to fly all over the screen.
- I don’t know how to fix the problem I am having with my network. At the moment, when one player moves his or her character, it actually moves the other player’s character, and vice versa.
You can view [this YouTube video][1] to see my problems in action.
Also, when I add gravity to the player/character in another test game (not the one in the video), it tips over based on the slope of the terrain. I want the character (which is a 2x1x1 cube) to stand upright, regardless of the terrain setting.
Here is some code from the game that is shown in the video:
Movement.js Code:
var speed:float = 0.2;
var rotateSpeed:float = 5;
function Start(){
if(!networkView.isMine)
{
enabled = false;
}
}
function Update(){
if(Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(Vector3.forward * speed);
}
if(Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(Vector3.back * speed);
}
if(Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(Vector3.up * rotateSpeed, Space.World);
}
if(Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.down * rotateSpeed, Space.World);
}
}
NetworkManagerScript.js Code:
var playerPrefab:GameObject;
var spawnObject:Transform;
var gameName:String = "CJay7_Test";
private var refreshing:boolean;
private var hostData:HostData[];
private var buttonX:float;
private var buttonY:float;
private var buttonWidth:float;
private var buttonHeight:float;
function Start(){
buttonX = Screen.width * 0.05;
buttonY = Screen.width * 0.05;
buttonWidth = Screen.width * 0.15;
buttonHeight = Screen.width * 0.15;
}
function startServer(){
Network.InitializeServer(32, 25001, !Network.HavePublicAddress);
MasterServer.RegisterHost(gameName, "Test Game", "I am creating a test game from a tutorial. 'http://vimeo.com/33996023#'");
}
function refreshHostList() {
MasterServer.RequestHostList(gameName);
refreshing = true;
}
function Update(){
if (refreshing)
{
if (MasterServer.PollHostList().Length > 0)
{
refreshing = false;
Debug.Log(MasterServer.PollHostList().Length);
hostData = MasterServer.PollHostList();
}
}
}
function spawnCharacter(){
Network.Instantiate(playerPrefab, spawnObject.position, Quaternion.identity, 0);
}
//MESSAGES
function OnServerInitialized(){
Debug.Log("Server Initialized");
spawnCharacter();
}
function OnConnectedToServer(){
spawnCharacter();
}
function OnMasterServerEvent(mse:MasterServerEvent){
if (mse == MasterServerEvent.RegistrationSucceeded)
{
Debug.Log("Registration Succeeded");
}
}
//GUI
function OnGUI(){
if(!Network.isClient && !Network.isServer){
if (GUI.Button(Rect(buttonX, buttonY, buttonWidth, buttonHeight), "Start Server"))
{
Debug.Log("Starting Server");
startServer();
}
if (GUI.Button(Rect(buttonX, buttonY * 1.2 + buttonHeight, buttonWidth, buttonHeight), "Refresh Hosts"))
{
Debug.Log("Refreshing");
refreshHostList();
}
if (HostData)
{
for (var i:int = 0; i < hostData.length; i++)
{
if(GUI.Button(Rect(buttonX * 2 + buttonWidth, buttonY * 1.2 + (buttonHeight * i), buttonWidth * 3, buttonHeight * .5), hostData*.gameName))*
-
{*
_ Network.Connect(hostData*);_
_ }_
_ }_
_ }_
_ }_
_}_
Again, please view [this YouTube video][1] to see my problems in video form. Thank you in advance for any constructive and helpful comments.
[1]: Problem with 3D Third-Person Multiplayer Game in Unity3D - YouTube*