Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
3.9 kB
2
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerBehaviour : MonoBehaviour
{
    // Start is called before the first frame update
    public float moveSpeed = 5f;
    public Rigidbody2D rb;
    SpriteRenderer tempRender;
    public Animator animator;

    public string playerFacing;

    Vector2 movement;

    public bool characterCanMove = true;
    float speed;
    bool isMoving = false;
    bool stayStill = false;

    bool MovingX = false;
    bool MovingY = false;
    float FacingX = 0f;
    float FacingY = 0f;

    void Awake()
    {
        tempRender = GetComponent<SpriteRenderer>();
        animator = GetComponent<Animator>();
        speed = animator.GetFloat("speed");
    }
    // Update is called once per frame
    void Update()
    {
        //Character Moving
        isCharacterMoving();
        if (!stayStill) { playerFacingF(); }
        //correctorIdle();
        canPlayerMove();

        //magia camara
        tempRender.sortingOrder = (int)Camera.main.WorldToScreenPoint(transform.position).y * -1;

        
    }

    private void FixedUpdate()
    {
        //Physics Calculations
        //rb.MovePosition(rb.position * movement.normalized * moveSpeed * Time.fixedDeltaTime);
        rb.velocity = movement.normalized * moveSpeed;
    }

    private void handleMovement()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        animator.SetFloat("Horizontal", movement.x);
        animator.SetFloat("Vertical", movement.y);
        animator.SetFloat("Speed", movement.sqrMagnitude);

        animator.SetBool("MovingX", MovingX);
        animator.SetBool("MovingY", MovingY);
        animator.SetFloat("FacingX", FacingX);
        animator.SetFloat("FacingY", FacingY);
    }


    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("Colisión!");   
    }


    void correctorIdle()
    {

        if (!isMoving) {
            if (playerFacing == "Left") { animator.Play("Player_Idle_Left", -1, 0f); }
            if (playerFacing == "Right") { animator.Play("Player_Idle_Right", -1, 0f); }
            if (playerFacing == "Up") { animator.Play("Player_Idle_Up", -1, 0f); }
            if (playerFacing == "Down") { animator.Play("Player_Idle_Down", -1, 0f); }
        }
    }

    void playerFacingF()
    {
        if (Input.GetAxisRaw("Horizontal") > 0.01)
        {
            playerFacing = "Right";
            MovingX = true;
            MovingY = false;
            FacingX = 1f;
        }

        if (Input.GetAxisRaw("Horizontal") < -0.01)
        {
            playerFacing = "Left";
            MovingX = true;
            MovingY = false;
            FacingX = -1f;
        }

        if (Input.GetAxisRaw("Vertical") < -0.01)
        {
            playerFacing = "Down";
            MovingY = true;
            MovingX = false;
            FacingY = -1f;
        }

        if (Input.GetAxisRaw("Vertical") > 0.01)
        {
            playerFacing = "Up";
            MovingY = true;
            MovingX = false;
            FacingY = 1f;
        }
    }

    void isCharacterMoving()
    {
        if (rb.velocity.magnitude > 0)
        {
            isMoving = true;
        }
        else isMoving = false;
    }

    void canPlayerMove() {

        if (characterCanMove)
        {
            //rb.constraints = RigidbodyConstraints2D.None; <--- grasioso para meme
            stayStill = false;
            handleMovement();
        }

        else
        {
            //rb.constraints = RigidbodyConstraints2D.FreezePosition;
            stayStill = true;
            animator.Play("Player_Idle_" + playerFacing, -1, 0f);
        }
    }


}