Hey guys, got a situation here that I can’t figure out.
Basically I have lights throughout the scene, if the player moves so far from the light, his sanity decreases, this works with one light.
When i duplicate the lights around the scene, things start to go a bit funny, only working with one light rather than calculating the distance from all of them (only the closest one would be important).
Hope that makes sense, here’s the script I’ve written so far, also I should say, I’ve trying to learn a lot about C# and unity lately, this is still the start of the journey for me so I’m by far not very good, but I’m surprised and happy about where this project is so far, It’s great learning!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DarknessScript : MonoBehaviour{
GameObject lightSource;
public float sanity = 100;
void Awake(){
lightSource = GameObject.FindGameObjectWithTag("LightSource");
//Trying to make lightSource contain anything that's tagged as "LightSource", will be multiple light sources in the scene
}
void Update(){
if(Vector3.Distance(transform.position, lightSource.transform.position) > 6){
//If I'm 15 units away from any of the light sources, I'm in the dark and losing sanity (Not scripted yet)
sanity -= Time.deltaTime;
print(sanity);
}
print(Vector3.Distance(transform.position, lightSource.transform.position));
}
}
I know this isn’t going to work by looking at the code, but I’m not entirely sure why or how to get around it.
I’m wondering if I need some sort of array to store ALL of the lights I want to interact with?
Many thanks,
Chris.
EDIT:
More to it.
Even when it’s just the one light being used, (I’m printing the value of sanity to console) the number starts counting down from 100 as expected, when I enter and exit the ‘zone’ (6 meters from the light) the sanity in the console seems to jump all over the place, it could be counting down from 100, I enter the zone and it stops as expected, when I leave the zone and approach the 6m mark, it’ll drop to 13, then bounce back up, and it gets even weirder when more lights are there.