Игрок дергается при прыжке
Я почитал в интернете и в форумах, как сделать правильную реализацию движения и управления камерой, в итоге у меня получился вот такой код на движение:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Movement : MonoBehaviour
{
public float Speed;
public float JumpForce;
private bool _isGrounded;
private Rigidbody _rb;
[SerializeField] private FloatingJoystick _js;
void Start()
{
_rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
MovementLogic();
}
private void MovementLogic()
{
float moveHorizontal = _js.Horizontal;
float moveVertical = _js.Vertical;
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.Translate(movement * Speed * Time.fixedDeltaTime);
}
private void JumpLogic()
{
if (_isGrounded)
{
_rb.AddForce(Vector3.up * JumpForce, ForceMode.VelocityChange);
}
}
public void Jump()
{
JumpLogic();
}
void OnCollisionEnter(Collision collision)
{
IsGroundedUpate(collision, true);
}
void OnCollisionExit(Collision collision)
{
IsGroundedUpate(collision, false);
}
private void IsGroundedUpate(Collision collision, bool value)
{
if (collision.gameObject.tag == ("Ground"))
{
_isGrounded = value;
}
}
}
И все работает хорошо, но при прыжке игрок дергается, я думаю это из-за того, что я делаю прыжок не в FixedUpdate, а просто вызываю его по нажатию UI кнопки. Как можно вызвать прыжок по нажатию кнопки UI, но в FixedUpdate.
В дополнение покажу код камеры вдруг в ней что то не так:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
public Transform cameraTarget;
public Transform playerModel;
public float minimumAngle;
public float maximumAngle;
public float mouseSensitivity;
public bool stickCamera;
[SerializeField] public DynamicJoystick _js;
void Start()
{
//Cursor.lockState = CursorLockMode.Locked;
//Cursor.visible = false;
}
void Update()
{
float aimX = _js.Horizontal;
float aimY = _js.Vertical;
cameraTarget.rotation *= Quaternion.AngleAxis(aimX * mouseSensitivity, Vector3.up);
cameraTarget.rotation *= Quaternion.AngleAxis(-aimY * mouseSensitivity, Vector3.right);
var angleX = cameraTarget.localEulerAngles.x;
Debug.Log(angleX);
if (angleX > 180 && angleX < maximumAngle)
{
angleX = maximumAngle;
}
else if (angleX < 180 && angleX > minimumAngle)
{
angleX = minimumAngle;
}
cameraTarget.localEulerAngles = new Vector3(angleX, cameraTarget.localEulerAngles.y, 0);
if (stickCamera)
{
playerModel.rotation = Quaternion.Euler(0, cameraTarget.rotation.eulerAngles.y, 0);
}
}
}
А также я использовал Asset Cinemachine Camera!
И еще раз уже отдельно продублирую вопрос: "Как сделать прыжок по нажатию UI Кнопки, в FixedUpdate?" Заранее спасибо!

