- Home /
How do I add gravity to my object, and how do I fix my network problem?
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: 1) 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. 2) 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 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[i].gameName))
{
Network.Connect(hostData[i]);
}
}
}
}
}
Again, please view this YouTube video to see my problems in video form. Thank you in advance for any constructive and helpful comments.
If you don't want it to rotate when hitting the terrain, just set your Rigidbody to Freeze the X,Y,Z rotations. You'll find this under the "Constraints" section of the Rigidbody. You need to use a Rigidbody if you want to use gravity. Your object flying all over the place is likely due to using Transform and Rotate ins$$anonymous$$d of rigidbody.velocity or rigidbody.AddForce to move the character. Or you have a rigidbody on a child object, ins$$anonymous$$d of the top parent.
As for the player issue, this seems to be due to your $$anonymous$$ovement script having no way to identify between the two players... So it's going to detect Player 2's Input and move as well.
i had a similar problem where it controlled the opposite player - with still no result. this was a while ago. perhaps you could shed some light on my topic too, and this link could be of reference to anyone else having troubles. maybe it will solve someone's problem.
http://answers.unity3d.com/questions/572938/players-not-moving-properly-network-multiplayer.html
Well, I'm not familiar with working with multiplayer features, that second part was just an educated guess. But I did a quick search and found this thread that seems to have 2 simple answers that would work depending on scenario: http://forum.unity3d.com/threads/25452-$$anonymous$$ultiplayer-Inputs
It looks like C$$anonymous$$7 is using the first method though, so not sure. $$anonymous$$aybe if he used this.enabled ins$$anonymous$$d, it would work. Just a guess though.
Hello,
Thank you for the response. I can't seem to find "Fixed Angle" in the Inspector for Rigidbody. Where is this located? I will check out the link for $$anonymous$$ultiplayer Inputs. I tried "this.enabled = false;", ins$$anonymous$$d of "enabled = false;", and it still didn't work. For some reason, I accidentally typed "this.enabledenabled = false;", and this worked for ONE of the screens, but not for both screens. I'm not sure why "networkView.is$$anonymous$$ine" is not see$$anonymous$$g to work on my game...
Answer by CJay7 · Jan 13, 2014 at 06:59 AM
I have solved both of my issues. I will try to remember to post my solutions tomorrow.
Just a re$$anonymous$$der to post what your solution was!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
setup a php master server 2 Answers
Unity networking tutorial? 6 Answers
(C#) Help with Multiplayer Features - Photon Unity Networking 0 Answers
Online Player Position 1 Answer