x


Multiplayer Cameras Problems

Hi, i'm new to unity, i'm playing with racing game tutorial, and trying to add multiplayer capabilities to it, i followed M2H network tutorial, and seem to understand it (to some extent), I'm creating car objects by Network.Instantiate also i modified Car.js, so that car would be controllable only by object owner

function GetInput()
{
    if (networkView.isMine) {
        throttle = Input.GetAxis("Vertical");
        steer = Input.GetAxis("Horizontal");
        CheckHandbrake();
    }
}

and this part seems to work. The problem that each car has a camera object assigned and camera has component CarCamera which needs target and is responsible for changing angles depenging on the car position, and when i create second car and second camera first camera loses it's focus and second one doesn't get one, so as a result while cars are individually controlled game cannot be played, because of that camera issue.

The script i use for instaniating players is:

function SpawnPlayer(){
    var player : Transform = Network.Instantiate(networkCarPrefab, Vector3(860, 102.2085, 878), transform.rotation, 0);
    var camera : Transform = Network.Instantiate(Main_Camera, Vector3(860, 102.2085, 878), transform.rotation, 0);
    var carCamera = camera.GetComponent("CarCamera");
    carCamera.target = player;
}

Would anyone point me into direction i should be digging ? Thx

more ▼

asked Jun 03 '10 at 09:11 AM

mihailt gravatar image

mihailt
11 1 1 5

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

2 answers: sort voted first

While, Cap provided some usefull insights, that was not the case.

The way i solved the problem, was not to instaniate a seperate camera object for each player, but to use one camera and to assign target for it on each player spawn. so the working code in my case looks like:

function SpawnPlayer(){
    var player : Transform = Network.Instantiate(networkCarPrefab, Vector3(860, 102.2085, 878), transform.rotation, 0);
    var camera = GameObject.Find("Main_Camera");
    var follow = camera.GetComponent("CarCamera");
    follow.target = player;
}
more ▼

answered Jun 03 '10 at 04:05 PM

mihailt gravatar image

mihailt
11 1 1 5

Oh, yea that'd work too heh.

Jun 03 '10 at 07:36 PM Cap
(comments are locked)
10|3000 characters needed characters left

As long as the camera's target is a non static member variable (ie it's declared outside any functions, at the very top of the script file as just var target : GameObject; or similar) you can assign your car to that in the Start or Awake events. Just use GetComponent("CarCamera").target = whatever, or use GetComponentInChildren if you parent the camera to the car's master game object. So just move your target variable outside any functions (and you may want to move the camera variable out too). Declare them at the top of the file using var name : GameObject; and then populate them further down.

Bear in mind that you'll want to declare the camera's target variable in its own script file attached to your camera, not the car, so look there for it instead of in your own spawn file.

more ▼

answered Jun 03 '10 at 11:37 AM

Cap gravatar image

Cap
311 8 9 18

(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:

x3014
x683

asked: Jun 03 '10 at 09:11 AM

Seen: 3095 times

Last Updated: Jun 03 '10 at 09:11 AM