i3e-asg2/Assets/Scripts/Raycasting.cs

50 lines
1.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Raycasting : MonoBehaviour
{
[SerializeField]
Transform playerCamera;
[SerializeField]
int seeDistance;
Interactable curretInteractable;
void Update()
{
bool Raycast = Physics.Raycast(
playerCamera.position,
playerCamera.TransformDirection(Vector3.forward),
out RaycastHit 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
{
curretInteractable = null;
}
}
else
{
curretInteractable = null;
}
}
void OnInteract()
{
if(curretInteractable!=null)
{
curretInteractable.Interact();
}
}
}