Hey i am new to networking and i did a tutorial on a very basic networking game and it worked perfectly. Now i am trying to make a FPS networking game and i ran into a problem. My problem is that when my player spawns into the map, he cant look around or move, i think i might be using the networkView.isMine in the wrong spots? Here is my script for mouse look:
function Update ()
{
if (networkView.isMine)
{
yrotation += Input.GetAxis("Mouse X") * looksensitivity;
xrotation -= Input.GetAxis("Mouse Y") * looksensitivity;
xrotation = Mathf.Clamp(xrotation, ymin, ymax);
currentxrotation = Mathf.SmoothDamp(currentxrotation, xrotation, xrotationv, looksmoothdamp);
currentyrotation = Mathf.SmoothDamp(currentyrotation, yrotation, yrotationv, looksmoothdamp);
transform.rotation = Quaternion.Euler(currentxrotation, currentyrotation, 0);
}
}
And here is my movement script:
function Update ()
{
if (networkView.isMine)
{
horizontalmovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if (horizontalmovement.magnitude > maxwalkspeed)
{
horizontalmovement = horizontalmovement.normalized;
horizontalmovement *= maxwalkspeed;
}
rigidbody.velocity.x = horizontalmovement.x;
rigidbody.velocity.z = horizontalmovement.y;
if (grounded)
{
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkdeaccelerationvolx, walkdeacceleration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkdeaccelerationvolz, walkdeacceleration);
}
transform.rotation = Quaternion.Euler(0, maincamera.GetComponent(MyMouseLook).currentyrotation, 0);
if (grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkacceleration, 0, Input.GetAxis("Vertical") * walkacceleration);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkacceleration * walkaccelairratio, 0, Input.GetAxis("Vertical") * walkacceleration * walkaccelairratio);
if (Input.GetButtonDown("Jump") && grounded)
{
rigidbody.AddForce(0, jumpvelocity, 0);
Debug.Log("Jumped!");
}
}
}
function OnCollisionStay (collision : Collision)
{
if (networkView.isMine)
{
for (var contact : ContactPoint in collision.contacts)
{
if (Vector3.Angle(contact.normal, Vector3.up) < maxslope)
{
grounded = true;
Debug.Log("On the Ground");
}
}
}
}
function OnCollisionExit ()
{
if (networkView.isMine)
{
grounded = false;
Debug.Log("In the Air");
}
}
This all works perfect in a single player FPS when i dont use the netoworkView.isMine but not in the networking one that im trying. Can anyone help on what i can do to make this all work? Thanks!
This is the script im using to connect:
var gameName: String = "Network_Test_1";
var player : GameObject;
private var btnX : float;
private var btnY : float;
private var btnW : float;
private var btnH : float;
private var refreshing : boolean = false;
private var hostData : HostData[];
function Start ()
{
btnX = Screen.width * 0.05;
btnY = Screen.width * 0.05;
btnW = Screen.width * 0.1;
btnH = Screen.width * 0.1;
}
function StartServer()
{
Network.InitializeServer(5, 2500, !Network.HavePublicAddress);
MasterServer.RegisterHost(gameName, "Networking Tutorial", "This is the Tutorial Test");
}
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 OnServerInitialized ()
{
Debug.Log("Server Initialized");
SpawnPlayer ();
}
function OnConnectedToServer ()
{
SpawnPlayer ();
}
function OnMasterServerEvent (mse : MasterServerEvent)
{
if (mse == MasterServerEvent.RegistrationSucceeded)
{
Debug.Log("Registered Server");
}
}
function SpawnPlayer ()
{
Network.Instantiate(player, transform.position, Quaternion.identity, 0);
}
function OnGUI ()
{
if (!Network.isClient && !Network.isServer)
{
if (GUI.Button(Rect(btnX, btnY, btnW, btnH), "Start Server"))
{
Debug.Log("Starting Server");
StartServer ();
}
if (GUI.Button(Rect(btnX, btnY * 1.2 + btnH, btnW, btnH), "Refresh"))
{
Debug.Log("Refreshing");
RefreshHostList ();
}
if (hostData)
{
for (var i : int = 0; i<hostData.length; i++)
{
if (GUI.Button(Rect(btnX * 1.5 + btnW, btnY * 1.2 + (btnH * i), btnW * 3, btnH * 0.5), hostData*.gameName))*
-
{*
_ Network.Connect(hostData*);_
_ }_
_ }_
_ }_
_ }_
_}*_