Untitled

 avatar
unknown
plain_text
a year ago
2.0 kB
11
Indexable
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting.ReorderableList;
using UnityEngine;

public class KarakterKontrol2 : MonoBehaviour
{
     float jumpForce=2.0f;
    float speed=1.0f;
    private bool jump;
    private float moveDirection;

    private bool moving;
    private bool grounded=true;
    private Rigidbody2D _rigidbody2D;
    private SpriteRenderer _spriteRenderer;
    private Animator anim;

     void Awake()
    {
        anim= GetComponent<Animator>();//caching ediyoruz
    }
     void Start()
    {
        _rigidbody2D = GetComponent<Rigidbody2D>();//caching ediyoruz
        _spriteRenderer= GetComponent<SpriteRenderer>();
       
    }
    private void FixedUpdate()
    {
        if(_rigidbody2D.velocity!=Vector2.zero)
        {
            moving = true;
        }else
        {
            moving = false;
        }
        _rigidbody2D.velocity = new Vector2(speed*moveDirection, _rigidbody2D.velocity.y);
        if(jump==true)
        {
            _rigidbody2D.velocity = new Vector2(_rigidbody2D.velocity.x, jumpForce);
            jump = false;
        }
    }
    private void Update()
    {
       

        if (grounded==true && (Input.GetKey(KeyCode.A)||Input.GetKey(KeyCode.D)))
        {
            if(Input.GetKey(KeyCode.A))
            {
                moveDirection = -1.0f;
                _spriteRenderer.flipX = true;
                anim.SetFloat("speed", speed);

            }
            else if(Input.GetKey(KeyCode.D)) {
                moveDirection = 1.0f;
                _spriteRenderer.flipX = false;
                anim.SetFloat("speed", speed);
            }
        }else if(grounded==true)
        {
            moveDirection = 0.0f;
            anim.SetFloat("speed", 0.0f);
        }
        if (grounded == true && (Input.GetKey(KeyCode.W)))
        {
            jump = true;

        }

    }

}
Editor is loading...
Leave a Comment