I am trying to make an fps. I recently learned Unity and am trying to raycast but its not working for some reason. If someone could explain why it isn’t working I would really appreciate it. Since I did just start learning Unity I was hoping to get an explanation in simple words as I don’t yet understand complex Unity topics. Here is my Code:
using UnityEngine;
using System.Collections;
public class PlayerShoot : MonoBehaviour
{
public PlayerWeapon weapon;
[SerializeField]
private Camera cam;
[SerializeField]
private LayerMask mask;
void Start ()
{
if (cam == null)
{
Debug.LogError("PlayerShoot: No Camera referenced");
this.enabled = false;
}
}
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
Shoot();
}
}
void Shoot ()
{
RaycastHit _hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, 200))
{
Debug.DrawRay(cam.transform.position, cam.transform.forward * 200);
Debug.Log("It Works!");
}
}
}