x


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.

more ▼

asked May 21 '12 at 01:36 PM

oliver-jones gravatar image

oliver-jones
2.5k 205 225 254

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered May 21 '12 at 01:42 PM

ExTheSea gravatar image

ExTheSea
2k 12 23 30

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?

May 21 '12 at 01:52 PM oliver-jones

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?

May 21 '12 at 02:05 PM ExTheSea

On my screen (being the admin), 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 admins do.

May 21 '12 at 05:07 PM oliver-jones

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. Maybe 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 minor 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.

May 21 '12 at 06:32 PM ExTheSea

Don't forget to tick "Run in background" or you get in trouble when you switch between two instances of your game ;)

May 21 '12 at 06:40 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1043
x804
x681
x224
x206

asked: May 21 '12 at 01:36 PM

Seen: 1185 times

Last Updated: May 22 '12 at 03:24 PM