Lens Flares flickering/disappearing when in front of a moving collider

I’ve been looking for answers to this for more than a year. I have two lense flares as headlights on my car. They work correctly when the car isn’t moving, but as soon as the car starts moving, the lense flares start flickering and as the speed increases, they disapper completely, seems like it’s sinking into the collider behind it.

When moving:
2237448--149217--LensflareBug2.png

When not moving:

2237448--149218--LensflareBug.png

Thanks in advance.

I would guess that the collider of the car are interfering with the lense flares. Collider will block the lense flares and on high speeds the physics may be not as correct and stuttering around.

1 Like

Yeah, I think that’s what happening, however its not only at high speeds, the flares sink" into the colllider once the car starts moving.

I guess I could make a mesh collider with “holes” right behind the lens flares, allowing them to move back a bit without touching the collider, while still blocking them from other angles where theyre supposed to be invisible…

Making holes in the colliders keeps the collider from blocking the flares(at least when the camera is directly in front of the car), but setting flare brightness using distance to camera (Which I need to make the flare look realistic ) also results in the flare disappearing…
I also have near clip plane set to 0 but it makes no difference!

Even if the distance to camera seems to be a constant number (when the camera isnt zooming in or out), the flare brightness still goes up and down uncontrollably…

I can’t find anything about this problem anywhere!

With this script attached to the flare, it behaves fairly normally and the flickering occur only above 170 km/h, but it’s barely noticeable.

// Headlights lense flare script
using UnityEngine;
using System.Collections;

public class Headlights : MonoBehaviour {
   
   
   public GameObject cameraMain;
   public float BrightnessMultiplier=4.0f;
   public float RelativeAngle;
   public float MinHeadlightAngle=-170.0f;
   public float MaxHeadlightAngle=9.0f;
   public float distance;
   LensFlare headlightFlare;
   public float brightness;

   
   void Start () {   
   if(cameraMain==null)cameraMain= GameObject.FindWithTag("MainCamera");
  headlightFlare = transform.GetComponent<LensFlare>();   
   }
   
   void Update () {
    distance= Mathf.Round(Vector3.Distance(cameraMain.transform.position,this.transform.position));
      brightness=BrightnessMultiplier/distance;

   // get positive or negative angle between cameraToHeadlight vector and headlight forward facing direction
   Vector3 Cam2Headlight = transform.position - cameraMain.transform.position;
  Vector3 referenceForward = transform.forward;
  Vector3 referenceRight= Vector3.Cross(Vector3.up, referenceForward);
  Vector3 newDirection = Cam2Headlight;
  float angle = Vector3.Angle(newDirection, referenceForward);
  float sign = Mathf.Sign(Vector3.Dot(newDirection, referenceRight));
   
  RelativeAngle = Mathf.Round( sign * angle);
     
   // show/hide lens flare depending on camera's angle relative to headlight       
     if(RelativeAngle > MinHeadlightAngle && RelativeAngle < MaxHeadlightAngle )
     {
       // if visible, adjust flare brightness depending on camera's distance to car
      if(brightness<2 && brightness> 0.8)headlightFlare.brightness= brightness;  
    }
      //if not visible set brightness to 0
     else{headlightFlare.brightness=0;
     
     }
     
}
       
}


[code]/
2 Likes