- Home /
Network - Character Controls
Hi all,
I'm continuing my quest to fully understand Unity's networking. Now, I've got a very simple scene working over a network. Basically, when someone connects, they are spawned a character, in which they can move up, down, left, right and what not. And that character is unique to that user.
The character uses the default third person controllers. Because I want my character to have unique features, I thought I would go ahead and build my own character control script... This is when my network scene started getting 'weird'. Its hard to explain, but it seemed like we were both controlling the same character, but we could both see where we were in the scene. If everyone moves around at the same time, the character would flash continuously back and forth between each of our positions.
Anyway, I assumed it was because of my character controller I built, and that it needs something unique in it? For a start, it doesn't use the 'Character Controller', nor the 'Character Motor', nor the 'FPSInput Controller'. It just has one simple script (that uses Translates and Slerps) a rigidbody, and a box collider.
What am I missing here?
Here is a snippet of my code:
if(Input.GetAxis("Vertical")){
//rotate player to face direction arrow
player.transform.rotation = Quaternion.Slerp(player.transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);
//them move player towards arrow direction
var translation : float = Input.GetAxis("Vertical") * playerMoveSpeed;
translation *= Time.deltaTime;
player.transform.Translate(0,0,translation);
}
Like I said, I'm trying to get my head around multiplayer... Its always fun to play games with other folks. So I would honestly appreciate any help, or advice. Cheers.
Answer by ExTheSea · May 21, 2012 at 01:42 PM
try adding to your script in a function Start()
if(!networkView.IsMine)
enabled=false;
this way if you added a networkView to the player gameobject the script makes so that you only controls your player. When you make a multiplayer game you should always add this code to scripts that define controls.
When you really whant to understand it read this:
http://unity3d.com/support/documentation/ScriptReference/NetworkView-isMine
Hope this helps.
Wow, well that worked like a charm... It seems very laggy though? and I'm running them both on my computer (server/client). I know this is a separate question, but why do you think that is?
Hm i don't think that has to do with the networkView and i also don't think with your movement system. Is it lagging on the screen you controlling it or only for the other one?
On my screen (being the ad$$anonymous$$), I have no lag, but the clients around me do. Same if I go on a client screen, the player doesn't lag, but the other clients and ad$$anonymous$$s do.
You should try it out with another computer/ a friend because if i try networking with to screen on the same computer nothing moves on the other screens. I mean the ones in which i'm not currently controlling anything. $$anonymous$$aybe it's just a reason of the two builds being on the same computer but there could also be a lagg caused by the netcode. In my FPS i sometimes noticed some $$anonymous$$or laggs but they could have also been caused by the bandwith. So try it with a friend/other computer and if that doesn't get rid of the laggs then you had to make your netcode bug-free.
Don't forget to tick "Run in background" or you get in trouble when you switch between two instances of your game ;)