PlayerController.cs 3.11 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
    public float distance = 10.0f;


    float velocityY = 0.0f;
    const float gravity = 9.8f;

    public HookBehaviour hook;

    private Rigidbody rb;
	private CapsuleCollider col;
	private DistanceJoint3D joint; 
    private bool isWired;



	[SerializeField]
	private Transform groundChecker;
	[SerializeField]
	private LayerMask groundMask;
	private bool IsGrounded
	{
		get {
			return Physics.OverlapSphere(groundChecker.position, 0.01f, groundMask).Length > 0;
		}
	}

	[SerializeField]
	private LayerMask enemyMask;

#if UNITY_EDITOR
    private void OnDrawGizmos()
    {
		Gizmos.color = Color.red;
        Gizmos.DrawLine(transform.position, Camera.main.transform.forward.normalized * 200);

		if (!IsGrounded)
			Gizmos.color = Color.green;
		Gizmos.DrawSphere(groundChecker.position, 0.1f);
	}
#endif

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        joint = GetComponent<DistanceJoint3D>();
        rb = GetComponent<Rigidbody>();
		col = GetComponent<CapsuleCollider>();

		//col.enabled = !IsGrounded;
		//cc.enabled = IsGrounded;
    }
    private void Update()
    {
        float vertical = Input.GetAxis("Vertical");
        float horizontal = Input.GetAxis("Horizontal");

		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit[] enemyHits = Physics.SphereCastAll(ray, 3, 50, enemyMask);
		Vector3[] positions = new Vector3[enemyHits.Length];
		for(int i = 0; i < enemyHits.Length; i++)
		{
			positions[i] = enemyHits[i].collider.transform.position;
		}

		IngameUIManager.inst.UpdateTargetLockedUIs(positions);


		if (Input.GetMouseButton(0))
		{
			foreach (var enemyHit in enemyHits)
			{
				Debug.Log(enemyHit.collider.gameObject);
			}
		}

        if (Input.GetMouseButtonDown(1))
        {
            if (!hook.gameObject.activeSelf)
            {
                FireHook();
            }
            else
                ReturnHook();
        }
		
		if (IsGrounded)
		{
			rb.velocity = (horizontal * transform.right + vertical * transform.forward).normalized * 10.0f;
			if (Input.GetButtonDown("Jump"))
				rb.velocity += new Vector3(0, 5 -rb.velocity.y, 0);
		}
        else
        {
			rb.AddForce(horizontal * transform.right + vertical * transform.forward * 10.0f);
        }
	}

    private void LateUpdate()
    {

    }

    private void FireHook()
    {
        joint.enabled = false;
        hook.gameObject.SetActive(true);
        hook.transform.position = transform.position;
        hook.EnableHook(Camera.main.transform.forward.normalized * 200);
    }

    private void ReturnHook()
    {
        joint.enabled = false;
        hook.DisableHook();
        isWired = false;
        rb.useGravity = true;
        //cc.enabled = true;
    }

    public void ActiveWire()
    {
        joint.enabled = true;
        joint.distance = Vector3.Distance(transform.position, hook.transform.position);
        isWired = true;
        rb.useGravity = false;
        //cc.enabled = false;
    }
}