Hello,
I am having problems with a weapon pick up system in my game.
I think it’s a raycast problem since after testing i tried printing “weapon selected”
if the raycast hits the weapon.
If anyone has any ideas on how to fix this please share it.
Thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InteracionManager : MonoBehaviour
{
public static InteracionManager Instance { get; set; }
public Weapon hoveredWeapon = null;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else { Instance = this; }
}
private void Update()
{
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
GameObject objectHitByRaycast = hit.transform.gameObject;
if (objectHitByRaycast.GetComponent<Weapon>())
{
print("WeaponSelected");
hoveredWeapon = objectHitByRaycast.gameObject.GetComponent<Weapon>();
hoveredWeapon.GetComponent<Outline>().enabled = true;
if (Input.GetKeyDown(KeyCode.F))
{
WeaponManager.Instance.PickupWeapon(objectHitByRaycast.gameObject);
}
}
else
{
if (hoveredWeapon) hoveredWeapon.GetComponent<Outline>().enabled = false;
}
}
}
}