So I have to raycasts emitting from the player. One Raycast coming from the player which is used to interact with objects, and one raycast coming from the muzzle of the gun which is used for the player to shoot. However, the raycast from the muzzle is way too short, and I cannot seem to lengthen it. To damage an object I have to get close to the object in order for it to register the hit.
This is the code for the player interact raycast
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInteract : MonoBehaviour
{
private Camera cam;
[SerializeField] //Serialize so it is visible in inspector
private float distance = 3f;
[SerializeField]
private LayerMask mask;
private PlayerUi playerUi;
private InputManager inputManager;
// Start is called before the first frame update
void Start()
{
cam = GetComponent<PlayerLook>().cam;
playerUi = GetComponent<PlayerUi>();
inputManager = GetComponent<InputManager>();
}
// Update is called once per frame
void Update()
{ playerUi.UpdateText(string.Empty);
//create a ray at center of camera shooting outwards
Ray ray = new Ray(cam.transform.position, cam.transform.forward);
Debug.DrawRay(ray.origin, ray.direction * distance);
RaycastHit hitInfo; //Variable to store collision info
if (Physics.Raycast(ray, out hitInfo,distance,mask)) //raycasting to center of the screen
{
if (hitInfo.collider.GetComponent<Interactable>() != null) //Checking to see if gameobject has interactable component
{
Interactable interactable = hitInfo.collider.GetComponent<Interactable>(); //if it does then we store interactable in a variable
playerUi.UpdateText(interactable.promptMessage); //Updating onscreen text
if (inputManager.onFoot.Interact.triggered)
{
interactable.BaseInteract(); //inside base interact function we are calling interactable function
}
}
}
}
This is for the gun
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Gun : MonoBehaviour
{
[SerializeField]
private GunData gunData;
[SerializeField]
private Transform muzzle;
float timeSinceLastShot;
private void Start()
{
PlayerShoot.shootInput += Shoot;
PlayerShoot.reloadInput += StartReload;
}
public void StartReload()
{
if (!gunData.reloading)
{
StartCoroutine(Reload());
}
}
private IEnumerator Reload()
{
gunData.reloading = true;
yield return new WaitForSeconds(gunData.reloadTime);
gunData.currentAmmo = gunData.magazineSize;
gunData.reloading = false;
}
private bool canShoot() => !gunData.reloading && timeSinceLastShot > 1f / (gunData.fireRate / 60f);
private void Shoot()
{
if (gunData.currentAmmo > 0)
{
if (canShoot())
{
if (Physics.Raycast(muzzle.position, transform.forward, out RaycastHit hitInfo, gunData.maxDistance))
{
IDamageable damageable = hitInfo.transform.GetComponent<IDamageable>();
damageable?.Damage(gunData.damage);
}
gunData.currentAmmo--;
timeSinceLastShot = 0;
OnGunShot();
}
}
}
private void Update()
{
timeSinceLastShot += Time.deltaTime;
Debug.DrawRay(muzzle.position, muzzle.forward);
}
private void OnGunShot()
{
}
}
In this image i used debug,drawray to show the two raycasts, the one coming from the gun is too short