- Home /
How do I get camera and movement controls for third person multiplayer to work correctly?
Hi everyone,
I'm pretty new to coding and using unity so hopefully there is an obvious solution to my problem. I'm trying to combine a few different tutorials and its not working out so hot. The problem I'm running into is when I add a second player to my server. I can control the camera with one instance but not the movement and with the other instance I can control the movement but not the camera.
Thank you very much for any help in advance!
These are the main blocks of code that I think are relevant, please let me know if you need to see something else.
TPCamera:
void Start()
{
//if(networkView.isMine)
//{
//make sure min < distance < max
Distance = Mathf.Clamp (Distance,DistanceMin,DistanceMax);
//set start distance
startDistance = Distance;
//set initial values for camera (position/distance)
Reset();
//}
//else
//{
//enabled = false;
//}
}
void LateUpdate()
{
//if(networkView.isMine)
//{
//check for targetlookat, if it doesn't exist don't update
if (TargetLookAt == null)
return;
HandlePlayerInput();
var count = 0;
do
{
CalculateDesiredPosition();
count++;
} while (CheckIfOccluded(count));
UpdatePosition();
//}
//else
//{
//enabled = false;
//}
}
If I uncomment the if statments for networkView.isMine the camera doesn't respond at all, even with just one player, and I'm very confused as to why. I also made sure that on my main camera I have a Network View Component added, which to my understanding lets the object be seen by other instances over the network, i could be wrong about that though ... This is all brand new to me.
One major difference I noticed with the networking tutorial I was following was that he has his camera attached to his player game object while my camera and player are separate game objects, could that be an issue?
Here is my TPController class:
void Awake()
{
if(networkView.isMine)
{
//allows us to reference this class in other classes?
CharacterController = GetComponent("CharacterController") as CharacterController;
Instance = this;
TPCamera.UseExistingOrCreateNewMainCamera();
}
else
{
enabled = false;
}
}
void Update()
{
if(networkView.isMine)
{
//if camera doesn't exist stop everything
if (Camera.mainCamera == null)
return;
GetLocomotionInput();
HandleActionInput();
TPMotor.Instance.UpdateMotor();
}
else
{
enabled = false;
}
}
I also have a MovementUpdate method which I put on my camera and player character:
public class MovementUpdate : MonoBehaviour {
private Vector3 lastPosition;
private Quaternion lastRotation;
private Transform myTransform;
void Start()
{
if(networkView.isMine)
{
myTransform = transform;
//ensure everyone sees player at correct location
networkView.RPC ("updateMovement",RPCMode.OthersBuffered,myTransform.position,myTransform.rotation);
}
else
{
enabled = false;
}
}
void Update()
{
//if player has moved then fire off RPC to update player's position and rotation
if(Vector3.Distance(myTransform.position, lastPosition) >= 0.1)
{
//capture player's position before RPC is fired and use this to determine in player has moved in if statement above
lastPosition = myTransform.position;
networkView.RPC ("updateMovement",RPCMode.OthersBuffered,myTransform.position,myTransform.rotation);
}
if(Quaternion.Angle(myTransform.rotation, lastRotation) >= 1)
{
//capture player's rotation before RPC is fired and use this to determine in player has turned in if statement above
lastRotation = myTransform.rotation;
networkView.RPC ("updateMovement",RPCMode.OthersBuffered,myTransform.position,myTransform.rotation);
}
}
[RPC]
void updateMovement (Vector3 newPosition, Quaternion newRotation)
{
transform.position = newPosition;
transform.rotation = newRotation;
}
}
These are the tutorials I've been working off of:
http://www.3dbuzz.com/training/view/3rd-person-character-system
http://www.youtube.com/watch?v=J4j4vlso4yc&list=PLC90282759FD90F77
@Howie, most moderators would just reject this question since, very long questions are never answered or even read. This forum is for really specific questions like "what is 82 degrees of dot product" or whatever.
Also,
the usual stock rejection response to "help with XYZ tutorial" is "question rejected, please ask the people at XYZ tutorial"
Finally you seem to be doing $$anonymous$$P networking here which is pretty advanced and it's difficult to answer "basic program$$anonymous$$g help" questions when one is dabbling in the most advanced fields.
So hopefully someone will magic a suggestion for you but really it is likely some other moderator will just close this
Your answer
Follow this Question
Related Questions
Have Camera Render First Person Arms But Not Body. 2 Answers
3rd person camera that avoids objects 0 Answers
2 players 1 game field in shooter/card game 0 Answers
Multiplayer click and spawn 1 Answer