The title explains it all pretty much. I am using the gun as a prefab so i had to hard code the camera in and it doesnt seem to work. ive moved the “fps cam =…” around so im not sure what the problem is. any help would be great. thanks!
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using Unity.VisualScripting.FullSerializer;
using UnityEngine;
public class Pistol : MonoBehaviour
{
public float damage = 5f;
public float magsize = 10f;
public float firerate = 60f; //Rounds per minute
public float range = 100f;
public GameObject player;
public Camera fpsCam;
void Awake()
{
}
void Update()
{
if (Input.GetButtonDown("Fire1") && Charger.canShoot == true)
{
Shoot();
Debug.Log("Fire");
}
}
void Shoot()
{
fpsCam = player.gameObject.GetComponentInChildren<Camera>(false);
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log("Object Hit" + hit.transform.name);
Debug.DrawRay(fpsCam.transform.position, fpsCam.transform.forward, Color.green);
}
}
}