Çözüldü Unity ile FPS Controller'ı düzgün çalışmıyor

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

Petrax

Picopat
Kek günü
Katılım
11 Kasım 2023
Mesajlar
113
Çözümler
2
Daha fazla  
Cinsiyet
Erkek
C#:
using UnityEngine;
using AFPC;

public class Controller : MonoBehaviour {

    public Overview overview;
    public float moveSpeed = 5f;
    public float jumpForce = 5f;
    public float gravity = -9.81f;
    public Transform groundCheck;
    public LayerMask groundMask;

    CharacterController controller;
    Vector3 velocity;
    bool isGrounded;

    private void Start () {

        controller = GetComponent<CharacterController>();
        QualitySettings.vSyncCount = 0;
        Cursor.lockState = CursorLockMode.Locked;

    }

    private void Update()
    {

        ReadInput();

        overview.Looking();

        overview.Aiming();

        overview.Shaking();

        isGrounded = Physics.CheckSphere(groundCheck.position, 0.2f, groundMask);

        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * moveSpeed * Time.deltaTime);

        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpForce * -2f * gravity);
        }

        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }

    private void FixedUpdate () {

    }

    private void LateUpdate () {

        overview.Follow (transform.position);
    }

    private void ReadInput () {
        overview.lookingInputValues.x = Input.GetAxis("Mouse X");
        overview.lookingInputValues.y = Input.GetAxis("Mouse Y");
        overview.aimingInputValue = Input.GetMouseButton(1);
    }

}

Bu kodda istediğimi eğer kamera ile sağa doğru bakıyorsam karakterimde sağa doğru dönsün ve ileri gitme tuşuna basınca baktığım yöne doğru gitsin ancak ben sağa doğru baksam bile ileri basınca karakterin önü neresiyse oraya gidiyor.
 
Son düzenleyen: Moderatör:
Çözüm
Sorun cidden global-local position ile mi alakalıymış. Nasıl çözdüğünüze dair biraz daha detaylı bilgi verirseniz sevinirim, merak ettim.
C#:
using UnityEngine;

public class MouseLook : MonoBehaviour
{
    public float speed = 5.0f;
    public float lookSpeed = 2.0f;
    public float jumpForce = 10.0f;
    public Transform groundCheck;
    public LayerMask groundMask;

    private CharacterController controller;
    private Vector3 moveDirection;
    private bool isGrounded;
    private float rotationX = 0;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        #region MouseLook
        isGrounded = Physics.CheckSphere(groundCheck.position, 0.2f, groundMask);

        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        Vector3 move = transform.right * horizontalInput + transform.forward * verticalInput;
        controller.Move(move * speed * Time.deltaTime);

        float mouseX = Input.GetAxis("Mouse X") * lookSpeed;
        float mouseY = Input.GetAxis("Mouse Y") * lookSpeed;

        // Yatay dönüş
        transform.Rotate(Vector3.up * mouseX);

        // Dikey dönüş
        rotationX -= mouseY;
        rotationX = Mathf.Clamp(rotationX, -90f, 90f); // Dikey dönüşü sınırla
        transform.localRotation = Quaternion.Euler(rotationX, transform.localEulerAngles.y, 0f);
        #endregion MouseLook
       
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            moveDirection.y = jumpForce;
        }

        // Yer çekimi uygula
        moveDirection.y += Physics.gravity.y * Time.deltaTime;

        controller.Move(moveDirection * Time.deltaTime);
    }
}
Kodu baştan yazdım.
C#:
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * moveSpeed * Time.deltaTime);
Sorun burada hareket yönünü global position'a göre alması olabilir. Bunları player'a bağlıyorsunuz herhalde, local position'ı referans aldığınızdan emin olun.
 
Sorun burada hareket yönünü global Position'a göre alması olabilir. Bunları Player'a bağlıyorsunuz herhalde, local Position'ı referans aldığınızdan emin olun.

Tam olarak nasıl yapacağım yani?

Hallettim teşekkür ederim 🤗
 
Son düzenleyen: Moderatör:
Sorun cidden global-local position ile mi alakalıymış. Nasıl çözdüğünüze dair biraz daha detaylı bilgi verirseniz sevinirim, merak ettim.
C#:
using UnityEngine;

public class MouseLook : MonoBehaviour
{
    public float speed = 5.0f;
    public float lookSpeed = 2.0f;
    public float jumpForce = 10.0f;
    public Transform groundCheck;
    public LayerMask groundMask;

    private CharacterController controller;
    private Vector3 moveDirection;
    private bool isGrounded;
    private float rotationX = 0;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        #region MouseLook
        isGrounded = Physics.CheckSphere(groundCheck.position, 0.2f, groundMask);

        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        Vector3 move = transform.right * horizontalInput + transform.forward * verticalInput;
        controller.Move(move * speed * Time.deltaTime);

        float mouseX = Input.GetAxis("Mouse X") * lookSpeed;
        float mouseY = Input.GetAxis("Mouse Y") * lookSpeed;

        // Yatay dönüş
        transform.Rotate(Vector3.up * mouseX);

        // Dikey dönüş
        rotationX -= mouseY;
        rotationX = Mathf.Clamp(rotationX, -90f, 90f); // Dikey dönüşü sınırla
        transform.localRotation = Quaternion.Euler(rotationX, transform.localEulerAngles.y, 0f);
        #endregion MouseLook
       
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            moveDirection.y = jumpForce;
        }

        // Yer çekimi uygula
        moveDirection.y += Physics.gravity.y * Time.deltaTime;

        controller.Move(moveDirection * Time.deltaTime);
    }
}
Kodu baştan yazdım.
 
Çözüm

Yeni konular

Geri
Yukarı