Any way to increase raycast range?
I'm currently making an FPS game in which I'm using Raycast as some sort of "hitscan" to deal damage to the enemies. The problem I'm having is the raycast only has a range of 100. And as the map is quite big, such a small range doesn't really work. Any way to increase the raycast range?
The following code is the for the shooting where the Raycast is being used.
void Shoot()
{
currAmmo--;
nextTimeToFire = Time.time + 1f/database.weapons[id].fireRate;
RaycastHit hit;
if (Physics.Raycast(mainCam.transform.position, mainCam.transform.forward, out hit, database.weapons[id].range))
{
if(hit.transform.tag == "Enemy")
{
CharacterStats enemyStats = hit.transform.GetComponent<CharacterStats>();
enemyStats.TakeDamage(database.weapons[id].damage);
}
}
}
The weapon range is specific to each weapon that is stored in a database. The problem is that 100 is the max range, every value above 100 is the same. So is there a way to make the raycast range bigger? If not what is the best way to fix it without having to recode everything?
There is no reason the range of the raycast is capped to 100. Have you tried to call
Debug.DrawRay(mainCam.transform.position, mainCam.transform.forward * database.weapons[id].range , Color.red);
before calling Physics.Raycast
?
I am sure the length of the ray change according to the value of your weapons[id].range
(supposing the value effectively changes)
Your answer
Follow this Question
Related Questions
Range not working c# - shooting script using raycast 0 Answers
Basic question about Unity functionality 0 Answers
I can't get Physic.Raycast to work,My Physics.Raycast don't work 0 Answers
Cant Kill enemy with Raycast C# (SOLVED) 1 Answer
How can I make a third person camera collision script? 2 Answers