Untitled

Fail
 avatar
unknown
plain_text
2 years ago
3.2 kB
6
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_Control : MonoBehaviour
{
    public CharacterData data;
    public States states;
    public Animator animator;
    [Space]
    public LayerMask groundLayer;

    private Rigidbody2D rb;
    private Vector2 inputDirection, lastDirection;

    public float slidingTime, slideSpeed;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        Inputs();
    }

    private void FixedUpdate()
    {
        Check();

        if (!states.isSliding && !states.isAttacking)
        {
            Move();
        }

        if(states.isSliding)
        {
            Slide();
        }
    }

    void Inputs()
    {
        inputDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
       
        if(Input.GetAxis("Horizontal") != 0 && !states.isSliding)
        {
            lastDirection = inputDirection;
        }

        if (Input.GetKeyDown(KeyCode.Space) && isGrounded && !states.isJumping)
        {
            Jump();
        }

        if(Input.GetKeyDown(KeyCode.Mouse1) && isGrounded && !states.isJumping && !states.isSliding)
        {
            slidingTime = 0;
            slideSpeed = data.slideCurve.Evaluate(slidingTime);
            states.ChangeState("slide");
            rb.velocity = new Vector2(0.0f, rb.velocity.y);
        }
    }

    void Move()
    {
        float targetSpeed = inputDirection.x * data.moveMaxSpeed;
        float speedDiffrence = targetSpeed - rb.velocity.x;
        float accelerationRate = (Mathf.Abs(targetSpeed) > 0.01f) ? data.moveAcceleration : data.moveDecceleration;
        float movement = Mathf.Pow(Mathf.Abs(speedDiffrence) * accelerationRate, data.moveVelocityPower) * Mathf.Sign(speedDiffrence);

        rb.AddForce(movement * Vector2.right);
        //rb.velocity = new Vector2(inputDirection.x * movingSpeed, rb.velocity.y);
    }

    void Slide()
    {
        slidingTime += Time.deltaTime;
        slideSpeed = data.slideCurve.Evaluate(slidingTime);

        rb.velocity =  lastDirection * slideSpeed;

        if(slidingTime > 0.98f)
        {
            slidingTime = 0;
            slideSpeed = 0;
          states.ChangeState("");
        }
    }

    void Jump()
    {
        rb.AddForce(Vector3.up * data.jumpForce, ForceMode2D.Impulse);
        states.ChangeState("jump");
    }

    void Check()
    {
        //Falling
        if (states.isJumping && rb.velocity.y < -0.5f || rb.velocity.y < -0.5f && !isGrounded)
        {
            states.ChangeState("fall");
        }

        //Grounded
        if(states.isFalling && isGrounded && !states.isSliding || rb.velocity.y != 0 && isGrounded && !states.isSliding)
        {
            states.ChangeState("");
        }
    }

    bool isGrounded
    {
        get
        {
            return Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y + 0.2f), Vector2.down, 0.4f, groundLayer);
        }
    }
}
Editor is loading...