i3e-asg2/Assets/Scripts/CharactorControl.cs
2024-07-02 10:42:09 +08:00

93 lines
2.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using Unity.VisualScripting;
using UnityEngine;
public class CharacterControl : MonoBehaviour
{
[SerializeField]
Transform playerCamera;
[SerializeField]
float seeDistance;
Interactable curretInteractable;
Pickable currentPickable;
public GunPickable CurrentGun;
public GameObject holdingGun;
public RaycastHit hitInfo;
public static bool handsFull;
[SerializeField]
private GameObject hand;
void Update()
{
bool Raycast = Physics.Raycast(
playerCamera.position,
playerCamera.TransformDirection(Vector3.forward),
out hitInfo,
seeDistance
);
Debug.DrawRay(
playerCamera.position,
playerCamera.TransformDirection(Vector3.forward) * seeDistance,
Color.green
);
if (Raycast)
{
Debug.Log(hitInfo.transform.name);
if (hitInfo.transform.TryGetComponent<Interactable>(out curretInteractable)) { }
else if (hitInfo.transform.TryGetComponent<Pickable>(out currentPickable)) { }
else
{
curretInteractable = null;
currentPickable = null;
}
}
else
{
curretInteractable = null;
currentPickable = null;
}
}
void OnInteract()
{
if (curretInteractable != null)
{
curretInteractable.Interact();
}
if (currentPickable != null)
{
currentPickable.PickUp();
if (currentPickable.TryGetComponent<GunPickable>(out CurrentGun))
{
holdingGun = CurrentGun.gameObject;
}
}
}
void OnShoot()
{
Debug.Log("Shoot");
if (CurrentGun != null)
{
CurrentGun.GetComponent<Gun>().Shoot();
}
}
void OnReload()
{
if (CurrentGun != null)
{
CurrentGun.GetComponent<Gun>().Reload();
}
}
void OnDrop()
{
CurrentGun.transform.GetComponent<Pickable>().Drop();
}
}