Hello,
I’m using Mirror and I’m trying to get the animation to play when the player aims.
It works on the client side, but it glitches and it eventually stops working on the server side.
I tried many ways such as using [Command]
and [ClientRpc]
, but it always goes back to this problem. I even tried a different input system and animation but that doesn’t do anything. I’m new to multiplayer and I’m in desperate need for help as I can’t find any solution to this problem.
public class ThridPersonShooterController : NetworkBehaviour {
[SerializeField] private CinemachineVirtualCamera aimVirtualCamera;
[SerializeField] private float normalSensitvity;
[SerializeField] private float aimSensitvity;
[SerializeField] private LayerMask aimColliderLayerMask = new LayerMask();
private ThirdPersonController thirdPersonController;
private StarterAssetsInputs starterAssetsInputs;
private Transform hitTransform;
private Animator animator;
Vector3 mouseWorldPosition;
private void Awake() {
thirdPersonController = GetComponent<ThirdPersonController>();
starterAssetsInputs = GetComponent<StarterAssetsInputs>();
animator = GetComponent<Animator>();
}
private void Update() {
Aim();
}
private void Aim() {
mouseWorldPosition = Vector3.zero;
Vector2 screenCenterPoint = new Vector2(Screen.width / 2f, Screen.height / 2f);
Ray ray = Camera.main.ScreenPointToRay(screenCenterPoint);
hitTransform = null;
if (Physics.Raycast(ray, out RaycastHit raycastHit, 999f, aimColliderLayerMask)) {
mouseWorldPosition = raycastHit.point;
hitTransform = raycastHit.transform;
}
if (starterAssetsInputs.aim) {
aimVirtualCamera.gameObject.SetActive(true);
thirdPersonController.SetSensitvity(aimSensitvity);
thirdPersonController.SetRotateOnMove(false);
animator.SetLayerWeight(1, Mathf.Lerp(animator.GetLayerWeight(1), 1f, Time.deltaTime * 10f));
Vector3 worldAimTarget = mouseWorldPosition;
worldAimTarget.y = transform.position.y;
Vector3 aimDirection = (worldAimTarget - transform.position).normalized;
transform.forward = Vector3.Lerp(transform.forward, aimDirection, Time.deltaTime * 20f);
} else {
aimVirtualCamera.gameObject.SetActive(false);
thirdPersonController.SetSensitvity(normalSensitvity);
thirdPersonController.SetRotateOnMove(true);
animator.SetLayerWeight(1, Mathf.Lerp(animator.GetLayerWeight(1), 0f, Time.deltaTime * 10f));
}
}