Search Unity

Doom-like sprites getting looked at by different cameras

Discussion in 'General Graphics' started by citizengoosevv, Nov 2, 2020.

  1. citizengoosevv

    citizengoosevv

    Joined:
    Jun 27, 2019
    Posts:
    11
    I want to achieve billboard sprites like in doom... but there's a twist. My game is gonna have security cameras you can see on little screens throughout the level. The billboard effect has to be projected to my main camera and all the secondary cameras as well.

    So I have to find an alternate path to billboarding, one that doesnt just turn the sprites around and face the player.

    How can I do this?
     
  2. citizengoosevv

    citizengoosevv

    Joined:
    Jun 27, 2019
    Posts:
    11
    Screw it, I solved it.

    So attach this to all the cameras:

    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3. public class CameraPreRender : MonoBehaviour {
    4.      public delegate void PreCullEvent();
    5.      public static PreCullEvent onPreCull;
    6.      void OnPreCull() {
    7.          if (onPreCull != null) {
    8.              onPreCull();
    9.          }
    10.      }
    11. }
    and this to the billboards:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class billboard : MonoBehaviour {
    4.      public Sprite fr, bc, lf, ri;
    5.       public SpriteRenderer theSR;
    6.      void OnEnable() {
    7.          CameraPreRender.onPreCull += MyPreCull;
    8.      }
    9.      void OnDisable() {
    10.          CameraPreRender.onPreCull -= MyPreCull;
    11.      }
    12.      void MyPreCull() {
    13.          //we want to look back
    14.          Vector3 difference = Camera.current.transform.position - transform.position;
    15.          difference.y = 0;
    16.          transform.LookAt(transform.position - difference, Camera.current.transform.up);
    17.  
    18.          var dir = Camera.current.transform.position - transform.position;
    19.           var enemyAngle = Mathf.Atan2(dir.z, dir.x) * Mathf.Rad2Deg;
    20.           if (enemyAngle < 0.0f){
    21.               enemyAngle += 360;
    22.               }
    23.          
    24.           if (enemyAngle >= 315f || enemyAngle < 45f){theSR.sprite = ri;}
    25.              
    26.           else if (enemyAngle >= 45f && enemyAngle < 135f){theSR.sprite = fr;}
    27.              
    28.           else if (enemyAngle >= 135f && enemyAngle < 225f){theSR.sprite = lf;}
    29.              
    30.           else if (enemyAngle >= 225f && enemyAngle < 315f){theSR.sprite = bc;}
    31.          
    32.      }
    33. }
    34.  
    You can play around with the angles at the end of the second script, you could make it 8 directional or more, but for my project i need 4