networking issues, camera changes / converting answer in C# to UnityScript

im having almost the same exact issue as this guy, http://answers.unity3d.com/questions/12006/camera-and-control-problems-with-network

but i'd like an answer in javascript not C# please so i can read it

You might also be interested in reading the answer to this question: What are the Syntax Differences in C# and Javascript?


Actually it's pretty easy to convert C# to JavaScript/UnityScript:

public class PlayerNetworkInit : MonoBehaviour
{

... this part is not necessary in JavaScript. It helps reading because it tells you that you're inheriting from MonoBehaviour (knowing this helps quite a bit when using the Scripting Reference). Anyways, just leave that out ... and of course also the closing "}" at the end of the example.

[SerializeField] Behaviour[] behavioursEnabledOnLocalClientsOnly;

[Something] is an attribute. I think those are written as @Something in UnityScript, or @Something(). Prefixing the attributes is a bit of an unusual formatting (but makes sense in this case). Nevertheless, I'd rather write (also include the "public" because this is publicly accessible):

[SerializeField]
public Behaviour[] behavioursEnabledOnLocalClientsOnly;

So I guess that would look like this in JavaScript/UnityScript:

@SerializeField
var behavioursEnabledOnLocalClientsOnly : Behaviour[];

So what you see here is: instead of prefixing the variable name with the type, you postfix it and add a colon ":". Also, you need the "var" in JavaScript in those cases.

void OnNetworkInstantiate(NetworkMessageInfo msg)

becomes in JavaScript:

function OnNetworkInstantiate(msg : NetworkMessageInfo)

So, you lose the information that this returns nothing ("void"). And you have to follow the "parameter name first, type last" syntax rule in JavaScript.

Finally, UnityScript has no "foreach"-loop and instead uses a special kind of "for"-loop.

So the loop in C#:

foreach (Behaviour behaviour in behavioursEnabledOnLocalClientsOnly)

Becomes the following in UnityScript:

for (var behaviour in behavioursEnabledOnLocalClientsOnly)

This is one of the reasons I personally dislike UnityScript: You lose the information that behaviour (which is just a random variable name) is actually of type Behaviour. The good news is that in UnityScript, you could also write:

for (var behaviour : Behaviour in behavioursEnabledOnLocalClientsOnly)


For the full UnityScript script, see the original question: Camera and control problems with network!