- Home /
Having camera attack upon spawning player?
How could I do this (using Java)? I have a working camera controller, but I want to add in a bit of networking, and have the camera attach to my cameraAttach object when the player spawns. So I added in a spawner object and have it set to spawn my character, but unless I drag the spawned character's cameraAttach, it doesn't work... Is there a way to do this in my code, or..?
Here's my code:
Spawnscript:
public var playerPrefab:Transform;
public var playerScripts:ArrayList = new ArrayList();
function OnServerInitialized() { //Now we'll spawn a server player. Spawnplayer(Network.player);
}
function OnPlayerConnected(newPlayer:NetworkPlayer) { //A player has connected to the server. Spawnplayer(newPlayer); }
function Spawnplayer(newPlayer:NetworkPlayer) { //This is only called on our server. var playerNumber:int = parseInt(newPlayer + "");
//Make a new object for our player. Don't forget that our server owns this. var myNewTrans:Transform = Network.Instantiate(playerPrefab, transform.position, transform.rotation, playerNumber);
//Next we'll get the networkview of the new transformation. var newObjectsNetworkview:NetworkView = myNewTrans.networkView;
//We need to keep track of this player so that we can properly destroy it when the time comes. playerScripts.Add(myNewTrans.GetComponent(PlayerScript2));
//Next we'll call an RPC on this new networkview and set the player that will control this one. newObjectsNetworkview.RPC("SetPlayer", RPCMode.AllBuffered, newPlayer);
}
function OnPlayerDisconnected(player:NetworkPlayer) { Debug.Log("Clean up after player " + player);
for(var script:PlayerScript2 in playerScripts) { //We've found our owner! if(player == script.owner) { //Now we'll move our buffered SetPlayer call. Network.RemoveRPCs(script.gameObject.networkView.viewID); //By destroying the game object, we destroy everything. Network.Destroy(script.gameObject); //Finally, we remove the player from the list. playerScripts.Remove(script); break; } }
//Now we'll remove the buffered RPC call for this player. var playerNumber:int = parseInt(player + ""); Network.RemoveRPCs(Network.player, playerNumber);
//The following won't really do anything since (in this sample) the players never instantiated //anything or buffered RPCs. So this is just here to give you an idea of how to do this in case //you create something where the players do instantiate and/or buffer. Network.RemoveRPCs(player); Network.DestroyPlayerObjects(player);
}
function OnDisconnectedFromServer(info:NetworkDisconnection) { Debug.Log("Resetting the scene the easy way."); Application.LoadLevel(Application.loadedLevel); }
And my camera controller:
//First, let's add the script to our Component menu in the Unity Editor.
@script AddComponentMenu("Camera-Control/Camera Controller")
//Let's declare some variables. var target:Transform; private var distance = 5.0; private var xSpeed = 250.0; private var ySpeed = 120.0; private var yMinLimit = 0; private var yMaxLimit = 80; private var cameraMode = "third"; private var zoomMinLimit = 2; private var zoomMaxLimit = 6;
private var x = 0.0; private var y = 5.0;
//Next we'll initialize our camera. function Start () { var angles = transform.eulerAngles; x = angles.y; y = angles.x; }
function LateUpdate () { //We'll check to see if our user has pressed the First Person toggle button. if(Input.GetButton("CameraToggleFirst")) { //Set the camera mode to first person. cameraMode = "first"; distance = 0.0; } //Now check to see if our user has toggled third person mode. if(Input.GetButton("CameraToggleThird")) { //Set the camera mode to third person. cameraMode = "third"; distance = 4.846878; }
//If we are in third person, we'll let the camera follow and be controlled. if(cameraMode == "third") { //We'll make variables to hold our character and camera rotation states at any given time. var characterRotation = target.transform.eulerAngles.y; var cameraRotation = transform.eulerAngles.y;
//Next we'll add the ability to zoom in...
if(Input.GetButton("CameraZoomIn") && distance > zoomMinLimit)
{
distance = distance -1 / (xSpeed * 0.02);
}
//...and out.
if(Input.GetButton("CameraZoomOut") && distance < zoomMaxLimit)
{
distance = distance +1 / (xSpeed * 0.02);
}
//Let's listen for the buttons and set the x and y accordingly.
x += Input.GetAxis("CameraX") * xSpeed * 0.02;
y -= Input.GetAxis("CameraY") * ySpeed * 0.02;
//Now we'll make sure that the character does not rotate beyond its limits.
y = ClampAngle(y, yMinLimit, yMaxLimit);
//Check to see if the user is moving character on either axis.
if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal"))
{
//We'll now set the rotation to move from the camera's rotation position to the character's at
//a speed of delta time * 3.
rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(y, characterRotation, 0), Time.deltaTime * 3);
position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
//Next we need to set x to characterRotation, otherwise we'll get a jump to the old position
//when we stop moving.
x = characterRotation;
}
else
{
//We'll rotate the camera from the current position to the position commanded by the user.
rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(y, x, 0), Time.deltaTime * 3);
position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
}
transform.rotation = rotation;
transform.position = position;
} else { characterRotation = target.transform.eulerAngles.y; x = characterRotation; rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, characterRotation, 0), Time.deltaTime 3); position = rotation Vector3(0.0,0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
//Now we need to ensure that we don't exceed the limits of the camera's rotation. static function ClampAngle (angle : float, min : float, max : float) { if (angle < -360) { angle += 360; } if (angle > 360) { angle -= 360; }
return Mathf.Clamp (angle, min, max);
}
Answer by Ramthor · Nov 05, 2010 at 07:30 PM
I don't know how it happened, but I had to fix the camera in a different scene (one very similar to the networking scene, except with no spawn point. Basically my single player scene.), and it somehow adjusted it so that the camera is targeting it when the network loads...