- Home /
Create interchanging Raycast cone for flashlight
So I'm creating a flashlight with 2 modes: Low intensity & high intensity
Low intensity mode gives a smaller range with a large cone of light and drains battery slowly High intensity mode gives a large range but with a narrow cone of light and drains battery fast
I want to make a raycast in the form of that cone of light so then I can create my collisions with enemy AI. However I have no clue as to how I can produce said raycast cone. Does anyone know how to do this / point me in the right direction?
This ray cast cone would of course change size depending on which intensity mode the flashlight was in so if high intensity mode then it'll be a narrow cone shape with a long range whilst in low intensity mode the raycast cone would be a very wide cone shape with a short range
Code for flashlight script is as below:
private Light flashlight;
//public AudioClip _switch;
// public AudioClip _batteryPickUp;
public int _maximumBatteryPower = 100;
public float _currentBatteryPower = 0f;
// 2 Modes of flashlight Low = small intensity short range, large angle. High = large intensity, small angle but longer range.
public float _lowPowerIntensityMode = 3f;
public float _lowPowerSpotAngle = 40f;
public float _lowPowerRange = 20f;
public float _highPowerIntensityMode = 6f;
public float _highPowerSpotAngle = 20;
public float _highPowerRange = 30f;
public float _lowDrainBatterySpeed = 2.5f;
public float _highDrainBatterySpeed = 5f;
public float _batteryBarLength;
public float _maxFlickerSpeed = 1f;
public float _minFlickerSpeed = 0.1f;
// Use this for initialization
void Start()
{ flashlight = GetComponentInChildren<Light>();
flashlight.enabled = false;
_currentBatteryPower = _maximumBatteryPower; }
// Update is called once per frame
void Update ()
{ _batteryBarLength = (Screen.width / 4) * (_currentBatteryPower / (float) _maximumBatteryPower);
// if (Input.GetButtonDown("Flashlight"))
// { //GetComponent<AudioSource>().PlayOneShot(_switch);
//flashlight.enabled = !flashlight.enabled; }
if (flashlight.enabled)
{ FlashlightOn(); }
if (_currentBatteryPower == 0)
{ StopCoroutine("FlashlightModifier");
flashlight.enabled = false; }}
private void FlashlightOn()
{ if (Input.GetMouseButton(1) && flashlight.enabled == true)
{ flashlight.intensity = _highPowerIntensityMode;
flashlight.spotAngle = _highPowerSpotAngle;
flashlight.range = _highPowerRange; }
else
{ flashlight.intensity = _lowPowerIntensityMode;
flashlight.spotAngle = _lowPowerSpotAngle;
flashlight.range = _lowPowerRange; }
if (flashlight.enabled)
{ _currentBatteryPower -= _lowDrainBatterySpeed * Time.deltaTime; }
if (Input.GetMouseButton(1) && flashlight.enabled == true)
{ _currentBatteryPower -= _highDrainBatterySpeed * Time.deltaTime; }
if(_currentBatteryPower <= 0)
{ _currentBatteryPower = 0; }
if (_currentBatteryPower < 10)
{ StartCoroutine("FlashlightModifier"); }
if (_currentBatteryPower > 10)
{ StopCoroutine("FlashlightModifier"); }}
IEnumerator FlashlightModifier()
{ while (true)
{ flashlight.enabled = true;
yield return new WaitForSeconds
(Random.Range(_minFlickerSpeed, _maxFlickerSpeed));
flashlight.enabled = false;
yield return new WaitForSeconds
(Random.Range(_minFlickerSpeed, _maxFlickerSpeed)); }}
public void AddBattery(int _batteryPowerAmount)
{ _currentBatteryPower += _batteryPowerAmount;
if (_currentBatteryPower >= _maximumBatteryPower)
{ _currentBatteryPower = _maximumBatteryPower; }
// if (_batteryPickUp != null)
//{ GetComponent<AudioSource>().clip = _batteryPickUp;
// GetComponent<AudioSource>().Play(); }}
//void OnGUI() // Change this to latest GUI in unity
//{ GUI.Box(new Rect(5, 35, _batteryBarLength, 20), "Battery");
}
Answer by Dragonfly3r · Dec 03, 2016 at 10:40 PM
I ended up fixing this myself. I 2 cone shapes in 3ds max. Removed the mesh renderer and mesh filter and turn the mesh collider into convex & istrigger.
In the code this is what I changed:
void Start()
{ flashlight = GetComponentInChildren<Light>();
_lowIntensityBeam.GetComponent<GameObject>();
_highIntensityBeam.GetComponent<GameObject>();
_lowIntensityBeam.gameObject.SetActive(false);
_highIntensityBeam.gameObject.SetActive(false);
flashlight.enabled = false;
_currentBatteryPower = _maximumBatteryPower; }
Void Update additions:
if (flashlight.enabled)
{ FlashlightOn();
if (_modeChange == false)
{
_lowIntensityBeam.gameObject.SetActive(true);
_currentBatteryPower -= _lowDrainBatterySpeed * Time.deltaTime;
}
else if (_modeChange == true)
{
_currentBatteryPower -= _highDrainBatterySpeed * Time.deltaTime;
}
}
else if (!flashlight.enabled)
{
_lowIntensityBeam.gameObject.SetActive(false);
_highIntensityBeam.gameObject.SetActive(false);
_modeChange = false;
}
if (_currentBatteryPower == 0)
{ StopCoroutine("FlashlightModifier");
flashlight.enabled = false;
_lowIntensityBeam.gameObject.SetActive(false);
_highIntensityBeam.gameObject.SetActive(false);
} }
private void FlashlightOn()
{ if (flashlight.enabled)
{
if (Input.GetMouseButtonDown(1) && _modeChange == false)
{
_modeChange = true;
flashlight.intensity = _highPowerIntensityMode;
flashlight.spotAngle = _highPowerSpotAngle;
flashlight.range = _highPowerRange;
_lowIntensityBeam.gameObject.SetActive(false);
_highIntensityBeam.gameObject.SetActive(true);
}
else if (Input.GetMouseButtonDown(1) && _modeChange == true)
{
_modeChange = false;
flashlight.intensity = _lowPowerIntensityMode;
flashlight.spotAngle = _lowPowerSpotAngle;
flashlight.range = _lowPowerRange;
_lowIntensityBeam.gameObject.SetActive(true);
_highIntensityBeam.gameObject.SetActive(false);
}
}
Answer by ElijahShadbolt · Nov 18, 2016 at 09:36 AM
You could loop through all your AIs that you want to test for Line of Sight in the cone, and check that the angle between the light's forward vector and the direction towards the AI is smaller than the SpotAngle you set, as well as that the AI is within a range. Then you could loop through each of those AIs and raycast towards the flashlight from their position to see if they indeed have Line of Sight.
LightLineOfSight.cs - attach to your flashlight (spot light)
using UnityEngine;
public class LightLineOfSight : MonoBehaviour
{
public GameObject[] targetAIs;
void Update()
{
Light flashlight = GetComponent<Light>();
if (flashlight == null)
return;
foreach (GameObject target in targetAIs)
{
Vector3 point = target.transform.position;
Vector3 direction = point - transform.position;
// if within range and cone angle
if (direction.magnitude <= flashlight.range && Vector3.Angle(transform.forward, direction) <= flashlight.spotAngle/2)
{
Debug.Log(target.name + " is in the light!");
Ray ray = new Ray(point, -direction);
RaycastHit[] hits = Physics.RaycastAll(ray, flashlight.range);
bool foundHit = false;
foreach (RaycastHit hit in hits)
{
if (hit.transform != transform && hit.transform != target.transform) // if not the flashlight nor that AI
{
Debug.Log("There is something between the flashlight and " + target.name);
foundHit = true;
break;
}
}
if (!foundHit)
{
Debug.Log(target.name + " has direct line of sight to the flashlight.");
}
}
}
}
}