using FishNet;
using FishNet.Object;
using FishNet.Object.Prediction;
using UnityEngine;
using UnityEngine.InputSystem;
using StarterAssets; //Call from the StarterAssets namespace
using FishNet.Serializing.Helping;
namespace FishNet.Example.Prediction.CharacterControllers
{
public class CharacterPrediction : NetworkBehaviour
{
#region Types.
public struct MoveData //AKA Replicate data
{
public float Horizontal;
public float Vertical;
public Quaternion Rotation;
public bool Jump;
}
public struct ReconcileData
{
public Vector3 Position;
public float VerticalVelocity;
public float SprintSpeed;
public ReconcileData(Vector3 position, float sprintSpeed) //Why is this here?
{
Position = position;
VerticalVelocity = 0f;
SprintSpeed = sprintSpeed;
}
}
#endregion
#region Serialized.
[SerializeField] private float targetSpeed;
[SerializeField] private float moveSpeed = 5f;
[SerializeField] private float sprintSpeed = 8f;
public float speedChangeRate = 10.0f; //How fast to accel and decel (Not working ATM)
public bool jumpQueued; //Used for if we want to jump
public bool isGrounded; //checking if controller is grounded
public StarterAssetsInputs input; //Use the StarterAssetsInputs script/class
[Header("Cinemachine")]
[Tooltip("The follow target set in the Cinemachine Virtual Camera that the camera will follow")]
public GameObject CinemachineCameraTarget;
[Tooltip("How far in degrees can you move the camera up")]
public float TopClamp = 90.0f;
[Tooltip("How far in degrees can you move the camera down")]
public float BottomClamp = -90.0f;
#if ENABLE_INPUT_SYSTEM && STARTER_ASSETS_PACKAGES_CHECKED
[SerializeField] public PlayerInput playerInput;
#endif
#endregion
#region Private.
private CharacterController characterController;
[SerializeField] private GameObject mainCamera;
public float jumpCooldown = 1.0f; // Setup timer for jumping ability
[SerializeField] private float speed;
[SerializeField] private float verticalVelocity;
private float rotationVelocity;
private float rotationSpeed = 1f;
private float cinemachineTargetPitch;
private Vector2 lastMousePosition; //Store the last mouse movement
InputAction jump = new InputAction(binding: "*/{Jump}");
#endregion```
#'Jump' not working for client.
2 messages · Page 1 of 1 (latest)
private void Awake()
{
InstanceFinder.TimeManager.OnTick += TimeManager_OnTick;
characterController = GetComponent<CharacterController>();
input = GetComponent<StarterAssetsInputs>();
// get a reference to our main camera
/*if (mainCamera == null)
{
mainCamera = GameObject.FindGameObjectWithTag("FPP_Camera"); //Changed to FPP Camera for testing.
}*/
}
public override void OnStartClient()
{
base.OnStartClient();
if (base.IsOwner) //if we own the camera object, enable it.
{
mainCamera.SetActive(true); //Main Camera is set to FPP Camera in editor.
}
//Disable the controller if not owner
characterController.enabled = (base.IsServer || base.IsOwner);
}
private void OnDestroy()
{
if (InstanceFinder.TimeManager != null)
{
InstanceFinder.TimeManager.OnTick -= TimeManager_OnTick;
}
}
private void Update()
{
//Store current mouse position
lastMousePosition = Mouse.current.position.ReadValue();
//Exit if we don't own
if (!IsOwner)
{
Debug.Log("Exiting update from Client: " + OwnerId);
return;
}
//Set if grounded based on Character Controller
//isGrounded = characterController.isGrounded;
//Check if we presssed space AND if grounded, then set jumpIsQueued
if (Keyboard.current.spaceKey.wasPressedThisFrame && characterController.isGrounded)
{
jumpQueued = true;
Debug.Log("CP: jumpqueued in update");
}
//Check for mouse input and rotation
PlayerRotation();
//Setting the amount for jumpCooldown based on time passed
jumpCooldown -= Time.deltaTime;
}
private void TimeManager_OnTick()
{
if (base.IsOwner)
{
Reconciliation(default, false);
CheckInput(out MoveData md);
Move(md, false);
}
if (base.IsServer)
{
Move(default, true);
ReconcileData rd = new ReconcileData()
{
Position = transform.position,
VerticalVelocity = verticalVelocity,
SprintSpeed = sprintSpeed
};
Reconciliation(rd, true);
}
}
private void PlayerRotation() //Turning the camera and player "Rotation"
{
if (input.look != null)
{
Debug.Log("Mouse Input in PlayerRotation()");
//Don't multiply mouse input by Time.deltaTime
float deltaTimeMultiplier = 1.0f;
cinemachineTargetPitch += input.look.y * rotationSpeed * deltaTimeMultiplier;
rotationVelocity = input.look.x * rotationSpeed * deltaTimeMultiplier;
// clamp our pitch rotation
cinemachineTargetPitch = ClampAngle(cinemachineTargetPitch, BottomClamp, TopClamp);
// Update Cinemachine camera target pitch
CinemachineCameraTarget.transform.localRotation = Quaternion.Euler(cinemachineTargetPitch, 0.0f, 0.0f);
// rotate the player left and right
transform.Rotate(Vector3.up * rotationVelocity);
}
}```