void Update()
{
if (!IsSpawned) return;
if (IsClient && IsOwner)
{
getInputs();
if (CameraClient == null)
return;
sendToServerRPC(
mouseScrollValueClient,
leftAltPressedClient,
isRightSideShoulderClient,
horizontalInputClient,
verticalInputClient,
xClient,
yClient,
playerPerspectiveClient,
speedClient,
distanceToCameraHolderClient,
lastDirectionFacedClient,
CameraHolderClientRef,
CameraClientRef,
transformRef);
//predictMovement();
}
}
//Then the code for the RPC call....
[ServerRpc(RequireOwnership =true)]
private void sendToServerRPC(
float mouseScrollValueClientRPC,
bool leftAltPressedClientRPC,
bool isRightSideShoulderClientRPC,
float horizontalInputClientRPC,
float verticalInputClientRPC,
float xClientRPC,
float yClientRPC,
perspective playerPerspectiveClientRPC,
float speedClientRPC,
float distanceToCameraHolderClientRPC,
Vector3 lastDirectionFacedRPC,
NetworkObjectReference cameraHolderRef,
NetworkObjectReference cameraRef,
NetworkObjectReference transformRef,
ServerRpcParams serverRpcParams = default)
{
//var clientIdRPC = serverRpcParams.Receive.SenderClientId;
Transform CameraHolderClientRPC;
Transform CameraClientRPC;
Animator playerAnimatorClientRPC;
Rigidbody ClientPlayerRigidbodyRPC;
NetworkObject CameraHolderObj;
NetworkObject CameraObj;
NetworkObject TransformObj;
cameraHolderRef.TryGet(out CameraHolderObj, NetworkManager.Singleton);
cameraRef.TryGet(out CameraObj, NetworkManager.Singleton);
transformRef.TryGet(out TransformObj, NetworkManager.Singleton);
CameraHolderClientRPC = CameraHolderObj.GetComponent<Transform>();
CameraClientRPC = CameraObj.GetComponent<Transform>();
ClientPlayerRigidbodyRPC = TransformObj.GetComponent<Rigidbody>();
playerAnimatorClientRPC = TransformObj.GetComponent<Animator>();
//var PlayerObject = NetworkManager.ConnectedClients[clientIdRPC].PlayerObject;
//var go = PlayerObject.gameObject;
////playerTransformRPC = go.transform;
//var ownedObjects = NetworkManager.ConnectedClients[clientIdRPC].OwnedObjects;
//foreach (var obj in ownedObjects)
//{
// if (obj.GetComponentInChildren<Camera>() != null)
// {
// CameraHolderClientRPC = obj.transform;
// CameraClientRPC = obj.GetComponentInChildren<Camera>().transform;
// }
// //else if (obj.GetComponent<Rigidbody>() != null
// // && obj.GetComponent<Animator>() != null)
// //{
// // playerTransformRPC = obj.transform;
// //}
//}
//playerAnimatorClientRPC = go.GetComponent<Animator>();
//ClientPlayerRigidbodyRPC = go.GetComponent<Rigidbody>();
Vector3 downVector = new Vector3(ClientPlayerRigidbodyRPC.position.x, ClientPlayerRigidbodyRPC.position.y + playerHeight, ClientPlayerRigidbodyRPC.position.z);
bool isGroundedRPC = Physics.Raycast(downVector, Vector3.down, playerHeight + .4f, whatIsGround);
//checkForIsGrounded(playerTransformRPC);
if (speedClientRPC > maxMovementSpeed)
{
speedClientRPC = maxMovementSpeed;
}
else if (speedClientRPC < minMovementSpeed)
{
speedClientRPC = minMovementSpeed;
}
switch (playerPerspectiveClientRPC)
{
case perspective.overTheShoulder:
overTheShoulder(ClientPlayerRigidbodyRPC,
mouseScrollValueClientRPC,
leftAltPressedClientRPC,
isRightSideShoulderClientRPC,
horizontalInputClientRPC,
verticalInputClientRPC,
xClientRPC,
yClientRPC,
playerPerspectiveClientRPC,
speedClientRPC,
distanceToCameraHolderClientRPC,
CameraHolderClientRPC,
CameraClientRPC,
playerAnimatorClientRPC,
isGroundedRPC);
break;
}
}
// Then the code for one of these methods, over the shoulder, currently.
private void overTheShoulder(Rigidbody ClientPlayerRigidbodyRPC,
float mouseScrollValueClientRPC,
bool leftAltPressedClientRPC,
bool isRightSideShoulderClientRPC,
float horizontalInputClientRPC,
float verticalInputClientRPC,
float xClientRPC,
float yClientRPC,
perspective playerPerspectiveClientRPC,
float speedClientRPC,
float distanceToCameraHolderClientRPC,
Transform CameraHolderClientRPC,
Transform CameraClientRPC,
Animator playerAnimatorClientRPC,
bool isGroundedRPC)
{
CameraHolderClientRPC.position = ClientPlayerRigidbodyRPC.position - (distanceToCameraHolderClientRPC * ClientPlayerRigidbodyRPC.transform.forward);
Vector3 VectorFromPlayerToTrueCameraLocation = CameraHolderClientRPC.position - ClientPlayerRigidbodyRPC.position;
Quaternion CurrentRotationQuaternion = Quaternion.Euler(0, xClientRPC, 0);
Vector3 movement = (ClientPlayerRigidbodyRPC.transform.forward * verticalInputClientRPC + ClientPlayerRigidbodyRPC.transform.right * horizontalInputClientRPC);
movement.y = 0;
if (isGroundedRPC)
{
ClientPlayerRigidbodyRPC.AddForce(10f * ClientPlayerRigidbodyRPC.mass * speedClientRPC * movement.normalized , ForceMode.Force);
}
else
{
ClientPlayerRigidbodyRPC.AddForce(.2f * ClientPlayerRigidbodyRPC.mass * speedClientRPC * movement.normalized, ForceMode.Force);
}
ClientPlayerRigidbodyRPC.transform.rotation = CurrentRotationQuaternion;
playerAnimatorClientRPC.SetFloat("MovementSpeed", ClientPlayerRigidbodyRPC.velocity.magnitude * 10);
Quaternion targetRotation = Quaternion.Euler(0, 0, 0);
if (movement.magnitude > 0)
{
targetRotation = Quaternion.LookRotation(movement.normalized);
}
float angleDifferenceFromCharacterToCamera = 0f;
angleDifferenceFromCharacterToCamera = GetSignedAngle(targetRotation, CurrentRotationQuaternion, new Vector3(0, 1, 0));
//angleDifferenceFromCharacterToCamera = Quaternion.Angle(targetRotation, rotation);
if ((angleDifferenceFromCharacterToCamera != 0) & (movement.magnitude != 0))
playerAnimatorClientRPC.SetFloat("AngleDifference", angleDifferenceFromCharacterToCamera);
else
playerAnimatorClientRPC.SetFloat("AngleDifference", 0);
// Calculate the camera offset based on the current distance
float xDistance = CameraHolderClientRPC.position.x - CameraHolderClientRPC.position.x;
float yDistance = CameraHolderClientRPC.position.y - CameraHolderClientRPC.position.y;
float currentDistance = Mathf.Sqrt(xDistance * xDistance + yDistance * yDistance);
float tValue = Mathf.InverseLerp(distanceMin, distanceMax, currentDistance);
float distanceFactor = Mathf.Lerp(1.0f, 0.5f, tValue);
float clampAngle = Mathf.Lerp(85.0f, 45f, tValue) * distanceFactor;
// Limit the y-angle to the clamp angle
yClientRPC = Mathf.Clamp(yClientRPC, -clampAngle, clampAngle);
CameraHolderClientRPC.position = ClientPlayerRigidbodyRPC.position + VectorFromPlayerToTrueCameraLocation;
Quaternion cameraRotation = CurrentRotationQuaternion;
CameraHolderClientRPC.rotation = cameraRotation * Quaternion.Euler(-yClientRPC, 0, 0);
//Quaternion upDownAngle = cameraRotation * Quaternion.Euler(-yServer, 0, 0);
//camController.rotationOffset = cameraRotation * Quaternion.Euler(-yClientRPC, 0, 0);
CameraClientRPC.rotation = cameraRotation * Quaternion.Euler(-yClientRPC, 0, 0);
float rightShift = isRightSideShoulderClientRPC ? shoulderShiftAmount : -shoulderShiftAmount;
float tiltAngle = Mathf.Deg2Rad * (yClientRPC);
float zDistance = CameraClientRPC.position.z - ClientPlayerRigidbodyRPC.position.z;
float flyingDistance = Mathf.Sqrt(xDistance * xDistance + yDistance * yDistance + zDistance * zDistance);
float extra_height = -((float)(flyingDistance * Mathf.Tan(tiltAngle)));
//float closer = -((float)(Mathf.Cos(tiltAngle) * flyingDistance));
//// Project the closer value onto the game's x-y plane
//Vector3 forward = CameraClientRPC.transform.forward;
//forward.y = 0;
//Vector3 right = CameraClientRPC.transform.right;
//if (!isRightSideShoulderClientRPC)
//{
// right = -CameraClientRPC.right;
//}
//right.y = 0;
//Vector3 projection = closer * forward + 1 * right;
//// Split the projection into x and y components
//float xProjection = projection.x;
//float yProjection = projection.z;
Vector3 rightVector = ClientPlayerRigidbodyRPC.transform.right * rightShift;
var totalChange = new Vector3(CameraHolderClientRPC.position.x,// + xProjection,
CameraHolderClientRPC.position.y + (float)(playerHeight + extra_height),
CameraHolderClientRPC.position.z);// + yProjection);
CameraClientRPC.position = totalChange + rightVector;
//if (isGroundedRPC)
//{
// if (ClientPlayerRigidbodyRPC.velocity.magnitude > maxMovementSpeed)
// {
// ClientPlayerRigidbodyRPC.AddForce((speedClientRPC * ClientPlayerRigidbodyRPC.velocity.normalized)
// - ClientPlayerRigidbodyRPC.velocity
// , ForceMode.VelocityChange);
// }
//}
Vector3 flatVel = new(ClientPlayerRigidbodyRPC.velocity.x, 0f, ClientPlayerRigidbodyRPC.velocity.z);
if (isGroundedRPC)
{
if (flatVel.magnitude > speedClientRPC)
{
Vector3 limitedVel = flatVel.normalized * speedClientRPC;
ClientPlayerRigidbodyRPC.velocity = limitedVel + new Vector3(0,ClientPlayerRigidbodyRPC.velocity.y,0);
}
}
//receiveClientRPC(
// ClientPlayerRigidbodyRPC.velocity,
// ClientPlayerRigidbodyRPC.position,
// mouseScrollValueClientRPC,
// leftAltPressedClientRPC,
// isRightSideShoulderClientRPC,
// horizontalInputClientRPC,
// verticalInputClientRPC,
// xClientRPC,
// yClientRPC,
// playerPerspectiveClientRPC,
// speedClientRPC,
// distanceToCameraHolderClientRPC,
// CameraHolderClientRPC.position,
// CameraHolderClientRPC.rotation,
// CameraClientRPC.position,
// CameraClientRPC.rotation,
// playerTransformRPC.position,
// playerTransformRPC.rotation,
// playerTransformRPC.forward);
}