Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ramthor · Nov 05, 2010 at 07:16 PM · cameraspawningattach

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);

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ramthor · Nov 05, 2010 at 07:17 PM 0
Share

Gah, the code box seems to have missed portions. Sorry.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Use lookAt and transform.translate at the same time 1 Answer

FPS Camera moving by my left mouse button 1 Answer

More Effective Camera Switching 1 Answer

How can i fix the camera in multiplayer game? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges