- Home /
Network FPS Scripting Help
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[i].gameName))
{
Network.Connect(hostData[i]);
}
}
}
}
}
For a start, do you have a network view attached to your game object ?
Then, ins$$anonymous$$d of always checking if it is your script or another player's, you should try :
function Awake () {
if (!networkView.is$$anonymous$$ine) enabled = false;
}
It avoids to enter the script at every frame...
Yes i do have it attached. And thanks ill try the awake function! Ill let u know how it goes.
So i used the awake function and it works great. But now im getting an error saying: "Failed to Connect. This is caused by an incorrect parameters, internal error or too many existing connections." Any help on what this means? And it cant be because there are too many connections, i set it to where 5 can join and theres only 1 person joining me.
Can you edit your question and post the code you are using to connect to the server ?
Answer by guby · Apr 03, 2013 at 06:25 AM
the same thing happened to me but i found out what was wrong it was probably you have too much things in your assets just delete some stuff that you dont need in your assets
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How can I get "!networkView.isMine" to function properly? 1 Answer
Does the position change need state synchronization? 2 Answers
One user controls all of them on network 1 Answer
Player 2 not spawning 1 Answer