Unity Karakter platforma atlayınca sabit kalıyor

fatihbuba

Centipat
Katılım
16 Mart 2024
Mesajlar
42
Daha fazla  
Cinsiyet
Erkek
Size şimdi platform hareketi kodunu atıyorum:
Kod:
using UnityEngine;

public class MovingPlatform : MonoBehaviour
{
    [Header("Hareket Ayarları")]
    [Tooltip("Platformun hareket hızı")]
    public float moveSpeed = 3f;

    [Tooltip("Platformun sağa-sola hareket mesafesi")]
    public float moveRange = 5f;

    [Header("Opsiyonel - Görsel Ayar")]
    [Tooltip("Başlangıç noktasını görmek için (opsiyonel)")]
    public bool showGizmos = true;

    private Vector3 startPos;
    private float direction = 1f;
    private Vector3 platformMovement;

    void Start()
    {
        startPos = transform.position;
    }

    void FixedUpdate()
    {
        MovePlatform();
    }

    void MovePlatform()
    {
        // Hareket miktarını hesapla
        platformMovement = Vector3.right * direction * moveSpeed * Time.fixedDeltaTime;
        transform.position += platformMovement;

        // Mesafe kontrolü
        float distanceFromStart = transform.position.x - startPos.x;

        if (Mathf.Abs(distanceFromStart) >= moveRange)
        {
            direction *= -1f;

            if (distanceFromStart > 0)
                transform.position = new Vector3(startPos.x + moveRange, transform.position.y, transform.position.z);
            else
                transform.position = new Vector3(startPos.x - moveRange, transform.position.y, transform.position.z);
        }
    }

    public Vector3 GetPlatformMovement()
    {
        return platformMovement;
    }

    void OnDrawGizmos()
    {
        if (!showGizmos) return;

        Vector3 center = Application.isPlaying ? startPos : transform.position;

        Gizmos.color = Color.yellow;
        Gizmos.DrawLine(
            new Vector3(center.x - moveRange, center.y, center.z),
            new Vector3(center.x + moveRange, center.y, center.z)
        );

        Gizmos.color = Color.green;
        Gizmos.DrawWireSphere(center, 0.2f);

        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(new Vector3(center.x - moveRange, center.y, center.z), 0.15f);
        Gizmos.DrawWireSphere(new Vector3(center.x + moveRange, center.y, center.z), 0.15f);
    }
}


bu da karakterin kodu
Kod:
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [Header("Hareket Ayarları")]
    public float moveSpeed = 5f;
    public float rotationSpeed = 10f;
    public float gravity = 15f;
    public float jumpForce = 8f;

    [Header("Better Jump Ayarları")]
    public float fallMultiplier = 2.5f;
    public float lowJumpMultiplier = 2f;

    [Header("Joystick Referansı")]
    public Joystick joystick;

    [Header("Platform Ayarları")]
    public float groundCheckDistance = 0.3f;

    private CharacterController characterController;
    private Vector3 moveDirection = Vector3.zero;
    private MovingPlatform currentPlatform;
    private Vector3 lastPlatformPos;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
        if (characterController == null)
            characterController = gameObject.AddComponent<CharacterController>();
    }

    void Update()
    {
        bool isGrounded = characterController.isGrounded;
        CheckForPlatform();

        float horizontalInput = 0f;
        float verticalInput = 0f;

        // Joystick input
        if (joystick != null)
        {
            horizontalInput = joystick.Horizontal;
            verticalInput = joystick.Vertical;
        }

        // Klavye input (test amaçlı)
        float keyboardH = Input.GetAxisRaw("Horizontal");
        float keyboardV = Input.GetAxisRaw("Vertical");
        if (Mathf.Abs(keyboardH) > 0.1f || Mathf.Abs(keyboardV) > 0.1f)
        {
            horizontalInput = keyboardH;
            verticalInput = keyboardV;
        }

        // Kamera yönüne göre hareket
        Vector3 forward = Camera.main.transform.forward;
        Vector3 right = Camera.main.transform.right;
        forward.y = 0f;
        right.y = 0f;
        forward.Normalize();
        right.Normalize();

        Vector3 moveDir = (forward * verticalInput + right * horizontalInput).normalized;
        Vector3 horizontalMove = moveDir * moveSpeed;

        // Yerdeyse y hızı hafif negatif
        if (isGrounded && moveDirection.y < 0)
            moveDirection.y = -2f;

        // Zıplama/gravity hesapları
        if (moveDirection.y < 0)
            moveDirection.y -= gravity * (fallMultiplier - 1) * Time.deltaTime;
        else if (moveDirection.y > 0 && !Input.GetButton("Jump"))
            moveDirection.y -= gravity * (lowJumpMultiplier - 1) * Time.deltaTime;

        moveDirection.y -= gravity * Time.deltaTime;

        // Platform hareketini uygula
        Vector3 platformMove = Vector3.zero;
        if (currentPlatform != null)
        {
            // Platformun çerçevedeki gerçek hareket miktarını hesapla
            platformMove = currentPlatform.transform.position - lastPlatformPos;
            lastPlatformPos = currentPlatform.transform.position;
        }

        // Tüm hareket birleşimi
        Vector3 totalMove = (horizontalMove + platformMove);
        totalMove.y = moveDirection.y;

        // Karakteri hareket ettir
        characterController.Move(totalMove * Time.deltaTime);

        // Yön rotasyonu
        if (moveDir != Vector3.zero)
        {
            Quaternion targetRotation = Quaternion.LookRotation(moveDir);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
        }

        // Platformdan ayrıldıysa sıfırla
        if (!isGrounded)
            currentPlatform = null;
    }

    void CheckForPlatform()
    {
        RaycastHit hit;
        Vector3 rayStart = transform.position + Vector3.up * 0.1f;

        if (Physics.Raycast(rayStart, Vector3.down, out hit, groundCheckDistance + 0.2f))
        {
            MovingPlatform platform = hit.collider.GetComponent<MovingPlatform>();

            if (platform != null)
            {
                if (currentPlatform != platform)
                {
                    currentPlatform = platform;
                    lastPlatformPos = platform.transform.position;
                }
            }
        }
        else
        {
            currentPlatform = null;
        }
    }

    public void Jump()
    {
        if (characterController.isGrounded)
        {
            moveDirection.y = jumpForce;
        }
    }
}
 

Technopat Haberler

Yeni konular

Geri
Yukarı