i3e-asg2/Assets/Scripts/CharactorControl.cs

93 lines
2.2 KiB
C#
Raw Normal View History

2024-07-01 03:11:10 +00:00
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;
2024-07-02 02:42:09 +00:00
public GunPickable CurrentGun;
public GameObject holdingGun;
2024-07-01 03:11:10 +00:00
public RaycastHit hitInfo;
public static bool handsFull;
2024-07-02 02:42:09 +00:00
2024-07-01 03:11:10 +00:00
[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)) { }
2024-07-02 02:42:09 +00:00
else if (hitInfo.transform.TryGetComponent<Pickable>(out currentPickable)) { }
2024-07-01 03:11:10 +00:00
else
{
curretInteractable = null;
currentPickable = null;
}
}
else
{
curretInteractable = null;
currentPickable = null;
}
}
void OnInteract()
{
if (curretInteractable != null)
{
curretInteractable.Interact();
}
if (currentPickable != null)
{
currentPickable.PickUp();
2024-07-02 02:42:09 +00:00
if (currentPickable.TryGetComponent<GunPickable>(out CurrentGun))
{
holdingGun = CurrentGun.gameObject;
}
2024-07-01 03:11:10 +00:00
}
}
void OnShoot()
{
Debug.Log("Shoot");
if (CurrentGun != null)
{
2024-07-02 02:42:09 +00:00
CurrentGun.GetComponent<Gun>().Shoot();
2024-07-01 03:11:10 +00:00
}
}
void OnReload()
{
if (CurrentGun != null)
{
2024-07-02 02:42:09 +00:00
CurrentGun.GetComponent<Gun>().Reload();
2024-07-01 03:11:10 +00:00
}
}
void OnDrop()
{
CurrentGun.transform.GetComponent<Pickable>().Drop();
}
}