using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public float speed = 400;
public float jumpforce = 5;
public LayerMask ground;
Animator animator;
Collider2D coll;
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
coll = GetComponent<Collider2D>();
}
private void FixedUpdate()
{
Movement();
switchanimator();
}
private void Update()
{
jump();
}
void Movement()
{
float horizontalmove = Input.GetAxis("Horizontal");
float direction = Input.GetAxis("Horizontal");
Debug.Log("horizontalmove: " + horizontalmove);
if(horizontalmove != 0)
{
rb.velocity = new Vector2(speed * horizontalmove * Time.deltaTime, rb.velocity.y);
animator.SetBool("isRunning", true);
}
else
{
animator.SetBool("isRunning", false);
}
if(direction != 0)
{
if(horizontalmove > 0)
{
transform.localScale = new Vector3(1, 1, 1);
}
if (horizontalmove < 0)
{
transform.localScale = new Vector3(-1, 1, 1);
}
}
}
void jump()
{
if (Input.GetButtonDown("Jump"))
{
rb.velocity = new Vector2(rb.velocity.x, jumpforce);
animator.SetBool("jump", true);
animator.SetBool("isidle", false);
}
}
void switchanimator()
{
if (animator.GetBool("jump"))
{
if(rb.velocity.y < 0)
{
animator.SetBool("jump", false);
animator.SetBool("fall", true);
}
}
else if (coll.IsTouchingLayers(ground))
{
animator.SetBool("fall", false);
animator.SetBool("isidle", true);
}
}
}