Range not working c# - shooting script using raycast
Im using debug to see the ray, the ray is on my target but it only inflicts damage when the target gets close to me. Is there something wron with the ray?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootingScript : MonoBehaviour {
public float weaponRange = 100f;
public Camera FPScamera;
public int minDamage = 15;
public int maxDamage = 30;
private AdvancedEnemyAI enemy;
private void Update()
{
Ray ray = FPScamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
RaycastHit hitInfo;
Debug.DrawRay(ray.origin, ray.direction * weaponRange, Color.red);
if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (Physics.Raycast(ray, out hitInfo, weaponRange))
{
if(hitInfo.collider.tag == "Enemy")
{
enemy = hitInfo.collider.GetComponent<AdvancedEnemyAI>();
enemy.TakeDamage(Damage());
}
}
}
}
private int Damage()
{
return Random.Range(minDamage, maxDamage);
}
}
Comment