using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public enum ControlType { PC, Phone }
public ControlType controlType;
public Joystick joystick;
public float speed;
public int health;
private Rigidbody2D rb;
private Vector2 moveInput;
private Vector2 moveVelocity;
private bool facingRight = true;
private Animator anim;
void Start()
{
rb = GetComponent();
anim = GetComponent();
if (controlType == ControlType.PC)
{
joystick.gameObject.SetActive(false);
}
}
void Update()
{
if (controlType == ControlType.PC)
{
moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
}
else if (controlType == ControlType.Phone)
{
moveInput = new Vector2(joystick.Horizontal, joystick.Vertical);
}
moveVelocity = moveInput.normalized * speed;
if (moveInput.x == 0)
{
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("isRunning", true);
}
if (!facingRight && moveInput.x > 0)
{
Flip();
}
else if (facingRight && moveInput.x < 0)
{
Flip();
}
}
void FixedUpdate()
{
rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
}
private void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
public void ChangeHealth(int healthValue)
{
health += healthValue;
}
}