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

public class PlayerRotator : MonoBehaviour
{

    public float rotation_speed = 10.0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        int layerMask = 1 << 8;

        // This would cast rays only against colliders in layer 8.
        // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
        layerMask = ~layerMask;

		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // Create ray from camera into the world through the mouse cursor position.
 
        RaycastHit hitInfo; // Empty var to store info about the thing we hit
 
        if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, layerMask)) // If raycast hit a collider...
        {
            Vector3 dir = Vector3.Normalize(transform.position - hitInfo.point);

			float delta = Vector3.Dot(dir,transform.forward);

			transform.Rotate(Vector3.up * delta * rotation_speed);

            //Debug.DrawRay(hitInfo.point,Camera.main.transform.position+new Vector3(1,1,1));

        }
    }
}
