How to render a particle in front of parent?
I am creating a torch, really simple and I want a glow coming from inside of the (opaque) torch, but the particle gets behind the object. https://imgur.com/u9pQG1c
And what I want for the glow to render in front of the Torch, yet behind walls and other scenery.
Answer by ifurkend · Nov 28, 2018 at 02:39 AM
There are various approaches to this. One of the laziest is to create a second "effect" camera parented to the main camera which only renders the objects of the TransparentFX layer (this layer should be removed from the main camera culling mask). Clear Flags should be "depth only" and higher "depth" (e.g. zero) than the main camera.
Obviously the glow will always appear before the walls, you can write your own simple lens flare script which involves raycasting the torch towards the main camera, make the glow transform-scale change to zero when the ray is blocked by any scene collider, and back to original scale when it's unblocked.
Another popular but not really foolproof method is to calculate the direction of the torch towards the camera and offset the torch position towards the main camera.
Attach the following script to your glow game object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamerawardOffset : MonoBehaviour {
[SerializeField] Camera mainCamera;
[SerializeField] Transform parent;
[SerializeField] Transform child;
[SerializeField] float _offset = 1f;
private Transform camTrans;
private Vector3 dir;
void Start () {
if (mainCamera == null)
mainCamera = Camera.main;
camTrans = mainCamera.transform;
if (parent == null)
parent = this.transform.parent;
if (child == null)
child = this.transform;
}
void Update () {
dir = Vector3.Normalize(camTrans.position - parent.position);
child.position = parent.position + dir * _offset;
//Courtesy of Unity Community http://wiki.unity3d.com/index.php/CameraFacingBillboard
child.LookAt(
parent.position + camTrans.rotation * Vector3.forward,
camTrans.rotation * Vector3.up
);
}
}
Your answer
Follow this Question
Related Questions
Particle System rendered in the camera view 2 Answers
Strange light diffusion on tilemap with particle system 0 Answers
How to make particles light up in sunlight regardless of their facing direction 0 Answers
Particles invisible in bottom half of game viewport 0 Answers
Need some assistance with spot/point lighting for GPU based particles 0 Answers