FPS character rigidbody rotation axis issue

3 weeks ago 25
ARTICLE AD BOX

Unwanted Rotation: When I jump or move sideways, the player object rotates on its own. If I lock the Y-axis in Rigidbody constraints, my mouse-look script stops working correctly.

I tried:

If I freeze the camera on the Y axis in Unity, the camera moves as if it were attached and it looks like it's experiencing an FPS drop.

using System; using UnityEngine; using UnityEngine.InputSystem; public class PlayerControl : MonoBehaviour { [SerializeField] Camera p_camera; [SerializeField] InputActionAsset p_inputMap; //Input// InputAction m_lookAction; InputAction m_moveAction; InputAction m_jumpAction; Vector2 moveActions; Vector2 lookActions; private Rigidbody rb; private float xRotation; private float yRotation; [SerializeField] float sensitivity; [SerializeField] float maxSpeed; [SerializeField] float airAccel; [SerializeField] float groundAccel; [SerializeField] float deAccel; [SerializeField] float jumpForce; [SerializeField] bool isGround; private void Awake() { rb = GetComponent<Rigidbody>(); //İmleç ayarları. Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; //Input maping m_lookAction = p_inputMap.FindAction("Player/Look"); m_moveAction = p_inputMap.FindAction("Player/Move"); m_jumpAction = p_inputMap.FindAction("Player/Jump"); m_lookAction.Enable(); m_moveAction.Enable(); m_jumpAction.Enable(); } // Her framede çalışır void Update() { Look(); } //Her fizik saniyesindi çalışıyor private void FixedUpdate() { Move(); Jump(); } //Gorund check yapıldığı yer private void OnTriggerStay(Collider other) { isGround = true; } private void OnTriggerExit(Collider other) { isGround = false; } void Look() { //Fare inputlarını güncellemem lookActions = m_lookAction.ReadValue<Vector2>(); //Mouse delta değerleri okuma float mouseX = lookActions.x * sensitivity * Time.deltaTime; float mouseY = lookActions.y * sensitivity * Time.deltaTime; //Mouse yukarı-aşağı xRotation -= mouseY; xRotation = Mathf.Clamp(xRotation, -89f, 89f); //Mouse sağa-sola yRotation = mouseX; //Kamerayı çevirme yukarı-aşağı p_camera.transform.localRotation = Quaternion.Euler(xRotation, 0, 0); //Karakteri sağa-sola çevirme transform.Rotate(Vector3.up * mouseX); } void Move() { // TODO: Bunnyhop,acseleriti,airacseleriti ve deaceleriti ekle. moveActions = m_moveAction.ReadValue<Vector2>(); float currentAccel = isGround ? groundAccel : airAccel; Vector3 moveDir = transform.forward * moveActions.y + transform.right * moveActions.x; Vector3 targetVelocity = moveDir * maxSpeed; Vector3 currentHorizontalVelocity = new(rb.linearVelocity.x, 0, rb.linearVelocity.z); Vector3 newHorizontalVelocity = Vector3.MoveTowards(currentHorizontalVelocity, targetVelocity, currentAccel); Vector3 newHorizontalDeAccel = Vector3.MoveTowards(currentHorizontalVelocity, targetVelocity, deAccel); float walkSpeed = newHorizontalVelocity.magnitude; Debug.Log(walkSpeed); if (moveActions.magnitude > 0) { rb.linearVelocity = new Vector3(newHorizontalVelocity.x, rb.linearVelocity.y, newHorizontalVelocity.z); } else { rb.linearVelocity = new Vector3(newHorizontalDeAccel.x, rb.linearVelocity.y, newHorizontalDeAccel.z); } } void Jump() { if (isGround) { if (m_jumpAction.IsPressed()) { rb.linearVelocity = new Vector3(rb.linearVelocity.x, jumpForce, rb.linearVelocity.z); } } } }
Read Entire Article