using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovementController : MonoBehaviour {

    public bool isHidden;

    [SerializeField]
    private float speed, rotSpeed;
    private CharacterController cc;
    private Vector3 moveDirection;
    private SpriteRenderer sp;

    void Start () {
        cc = GetComponent<CharacterController>();
        sp = GetComponentInChildren<SpriteRenderer>();
	}

    private void Update()
    {
        // (0, Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime, 0);
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
        transform.rotation = Quaternion.Euler(0,Vector3.Angle(Vector3.right,moveDirection),0);
        transform.LookAt(this.transform.position + moveDirection, Vector3.up);
        isHidden = (moveDirection == Vector3.zero && isHidden == true) ? true : false;
        if(!isHidden && sp.enabled == false)
        {
            sp.enabled = true;
        }
        //moveDirection = transform.TransformDirection(moveDirection);
        moveDirection = moveDirection * speed;
        cc.Move(moveDirection * Time.deltaTime);
        this.transform.position = new Vector3(this.transform.position.x,
                                              Mathf.Clamp(this.transform.position.y, 0f, 0.39f),
                                              this.transform.position.z);
    }

}
