- Home /
Lens Flare bug, sinks into colliders when moving
( Using Unity 3.5.7f6 )
I have two lense flares as headlights on my car. They work correctly at low speeds, but when exeeding a certain speed, the lense flare starts flickering and as the speed increases, disappers completely (because it's sinking into the collider behind it).
It looks like the brightness increases and decreases very fast and at the same time it fails to "keep up with the car" so it keeps sinking into the car collider behind it...
Edit: This is still bugging me, because the only way to get past this is to use the zoom value from the camera script to adjust the lens flare brightness, which doens't work for cameras that does't have a script attached and other objects thats far away from the camera.
I still think its really wierd that no one else has ever had this problem. Am I the only one who has tried to make headlights in Unity 3?
A car needs a collider, he headlights needs to be infront of that collider.... even calculating the distance from teh camera to the car results in this bug.
Here's a script I have written as a workaround, its a bit of a hack and not really a good solution:
using UnityEngine;
using System.Collections;
public class Headlights : $$anonymous$$onoBehaviour {
GameObject camera$$anonymous$$ain;
float Brightness=4.0f;
public float RelativeAngle;
public float $$anonymous$$inHeadlightAngle=-170.0f;
public float $$anonymous$$axHeadlightAngle=-30.0f;
void Start () {
camera$$anonymous$$ain= GameObject.FindWithTag("$$anonymous$$ainCamera");
}
void Update () {
// get positive or negative angle between cameraToHeadlight vector and headlight facing direction
Vector3 Cam2Headlight = transform.position - camera$$anonymous$$ain.transform.position;
Vector3 referenceForward = transform.forward;
Vector3 referenceRight= Vector3.Cross(Vector3.up, referenceForward);
Vector3 newDirection = Cam2Headlight;
float angle = Vector3.Angle(newDirection, referenceForward);
float sign = $$anonymous$$athf.Sign(Vector3.Dot(newDirection, referenceRight));
RelativeAngle = sign * angle;
// show/hide lens flare depending on camera's angle relative to headlight
CarCameras CarCameras = camera$$anonymous$$ain.GetComponent<CarCameras>();
float distance= CarCameras.distance;
LensFlare headlightFlare = transform.GetComponent<LensFlare>();
if(RelativeAngle > $$anonymous$$inHeadlightAngle && RelativeAngle < $$anonymous$$axHeadlightAngle )
{
// if visible, adjust flare brightness depending on camera's distance to car
headlightFlare.brightness= Brightness/distance;
}
// if not visible set brightness to 0
else{headlightFlare.brightness=0;}
}
}
I have found that calculating the distance to the camera
distance= Vector3.Distance(this.transform.position, camera$$anonymous$$ain.transform.position);
also results in the same flickering/ disappearing...
Seems that setting the brightness based on any calculation, makes the flare disappear/flicker...
Here's another solution. If I set up a bunch of box colliders around the flares, leaving a long tunnel behind them, the flares will stay on and act fairly normal, even at high speeds:
Answer by Stefan Alexander · Aug 21, 2014 at 08:35 PM
You need to make sure that the point lights (that is assuming that you are using point lights to generate your flares) are parented to the colliders in your car and have no physics components attached to them that may cause them to move back.
First, thanks for the reply:) Yes, that would seem like a plausible explanation, however I am not using point lights, just empty game objects with nothing but the lense flares, so they don't have any physics components attached either that could pull them back... It's very strange.
Have you considered updating you version of Unity? It is very probable that if this is a $$anonymous$$or bug, an update could correct this.
I would also consider using point lights an then add flares to them. (If the reason you're not using the point light is because you don't want them to emit any light, you can just go ahead and drag down the intensity slider without affecting the flares).
Thanks, yes, I have considered it, but installing a different version might mess up my project right? I upgraded my OS from Vista to 8 in january so decided to install Unity 4, but of course kept a backup of my projects. What happened was that most of the scenes were messed up, objects moved to different locations, scrips were full of errors due to obsolete stuff etc. I'll try to use pointlight as you suggested, I really hope that will fix it.
I added a spot light and set it up correctly, and it works!! No glitching! Seems that the bug only occurs when the flare is alone and not used in a light component! Simple but effective solution, should have thought about it myself, but thanks a lot man! :) Edit: No, that doesnt work! :(
Your answer
Follow this Question
Related Questions
Dirty Lens effect like BF3 2 Answers
how to operate 2 cameras simultaneously 1 Answer
replacing Lens Flare for URP in version 2019 1 Answer
Lens flare not showing in game view. 1 Answer
Adding lens flare to sun 0 Answers