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

public class Camfollow : MonoBehaviour
{
    // Start is called before the first frame update
    float yaw;
    float pitch;
    public Transform followTarget;
    public float sensitive = 1.0f;
    public float distance = 3.0f;
    public float height = 1f;
    float smoothheight;
    float realdistance;
    float smoothDistance;
    float cv;
    float cv1;
    void Start()
    {
        
    }

    // Update is called once per frame
    void LateUpdate()
    {
        ForCamper();
    }
    void ForCamper()
    {
        //Transform Follower = Global.FindChildRecursion(FollowTarget.transform,"joint_Char");
        realdistance = distance;
        RaycastHit hitinfo;
        yaw += Input.GetAxis("Mouse X") * sensitive;
        pitch -= Input.GetAxis("Mouse Y") * sensitive;
        smoothheight = Mathf.SmoothDampAngle(smoothheight, height, ref cv1, 0.3f);
        if (yaw > 360) yaw -= 360;
        if (yaw < 0) yaw += 360;
        if (pitch > 70f) pitch = 70f;
        if (pitch < -70f) pitch = -70f;
        transform.eulerAngles = new Vector3(pitch, yaw, 0);
        if (Physics.Raycast(followTarget.position, transform.position - (followTarget.position + Vector3.up * smoothheight), out hitinfo, distance))
        {
            if (/*hitinfo.transform.tag != "VaultPlace" && */hitinfo.transform.tag != "Player")
            {
                realdistance = (hitinfo.point - (followTarget.position + Vector3.up * smoothheight)).magnitude;
                smoothDistance = realdistance;
            }
        }
        smoothDistance = Mathf.SmoothDampAngle(smoothDistance, realdistance, ref cv, 0.3f);

        transform.position = followTarget.position - transform.forward * smoothDistance + Vector3.up * smoothheight;

    }
    void ForSlasher()
    {

    }
}
