Unity 3D TR Forum

Orjinalini görmek için tıklayınız: Space Zıplama Sorunu lütfen yardım
Şu anda (Arşiv) modunu görüntülemektesiniz. Orjinal Sürümü Görüntüle internal link
zıplama yapmaya çalıştığımda space'e abanıyorum anca 1 kere zıplıyor ve animasyon bile çalışmıyor yardımcı olursanız sevinirim




using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float runSpeed, jumpForce;
    public float jumpHeight = .4f;
    private float moveInput;

    private Rigidbody2D myBody;
    private Animator anim;

    public Transform groundCheck;
    public LayerMask groundLayer;

    private bool facingRight = true;

    private Vector3 range;

    void Awake()
    {
        myBody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    
    void FixedUpdate()
    {
        Movement();
        CheckCollisionForJump();
    }

    void Movement()
    {
        moveInput = Input.GetAxisRaw("Horizontal") * runSpeed;

        anim.SetFloat("Speed", Mathf.Abs(moveInput));

        myBody.velocity = new Vector2(moveInput, myBody.velocity.y);

        if(Input.GetKeyUp(KeyCode.Space))
            if(myBody.velocity.y > 0)
            {
                myBody.velocity = new Vector2(myBody.velocity.x, myBody.velocity.y * jumpHeight);
                anim.SetBool("Jump", true);
            }
            else
            {
                anim.SetBool("Jump", false);
            }

        if (moveInput > 0 && !facingRight || moveInput < 0 && facingRight)
            Flip();
    }

    void CheckCollisionForJump()
    {
        Collider2D bottomHit = Physics2D.OverlapBox(groundCheck.position, range, 0, groundLayer);

        if(bottomHit != null)
        {
            if(bottomHit.gameObject.tag == "Ground" && Input.GetKeyDown(KeyCode.Space))
            {
                myBody.velocity = new Vector2(myBody.velocity.x, jumpForce);
            }
        }
    }


    void Flip()
    {
        facingRight = !facingRight;

        Vector3 transformScale = transform.localScale;
        transformScale.x *= -1;
        transform.localScale = transformScale;
    }
}
Kod:
if(Input.GetKeyUp(KeyCode.Space))
           if(myBody.velocity.y > 0)
           {
               myBody.velocity = new Vector2(myBody.velocity.x, myBody.velocity.y * jumpHeight);
               anim.SetBool("Jump", true);
           }
           else
           {
               anim.SetBool("Jump", false);
           }
Burda if(Input.GetKeyUp(KeyCode.Space)) koşulunun süslü parantezleri olmadığından kod şu şekle dönüşüyor:

Kod:
if(Input.GetKeyUp(KeyCode.Space))
    if(myBody.velocity.y > 0)
    {
        myBody.velocity = new Vector2(myBody.velocity.x, myBody.velocity.y * jumpHeight);
        anim.SetBool("Jump", true);
    }
else
{
    anim.SetBool("Jump", false);
}

Yani eğer space'den elini çekmiyorsan her zaman anim.SetBool çalışıyor. Bu animasyon sorunundu. Şimdi zıplama sorununa gelirsek, mevcut olan velocity.y'yi bir değerle çarpmak yerine onun üstüne ekleme yapman daha sağlıklı olacaktır:

Kod:
myBody.velocity = new Vector2(myBody.velocity.x, myBody.velocity.y * jumpHeight); //Değil
myBody.velocity = new Vector2(myBody.velocity.x, myBody.velocity.y + (vector2.up * jumpHeight));
//Jump height değerini duruma göre artırıp azaltırsın
Birde boşluk tuşuna bastığında işlem yap, GetKeyUp değil, GetKeyDown, ardından aşağı doğru raycast gönderip zemine çarpıyorsa zıplama işlemini yapabilirsin.