- Home /
Billboarding in Unity 5 with Locked Z-Axis
I've been trying to get some billboarding done in a scene I've been working on.
.
I'm a level designer, my programming is mostly Python/Scripts for more utilitarian things, I feel completely lost in the world of C#/Java coordinates it seems.
.
Anyways, I was looking for some example code to meander through and poke at for some objective learning and I came across Neil Carter's code for billboarding, it works quite well with planes/quads. Awesome, so I was intending to lock the z-axis to prevent any graphical errors.
.
I was able to follow the code to the bottom without too much trouble, but in order to lock the z axis I would need to replace some code, problem is I have absolutely no idea what to replace at the bottom. I can't seem to wrap my head around the concept of Vector3, I figured it'd call (x,y,z) coordinates, but I'm really confused because in some references of code it seems like people call just 1 number/variable and leave the other 2 blank, but if I do that I get compiler errors.
.
If anyone could give me a hand I would greatly appreciate it. I'm always up for learning new things so if you'd like to explain the code in sections like I'm a 5 year old child I'm cool with that. I'm not the main programmer of the group, but he's on vacation and I kinda wanted to surprise him and learn some C# since it would take some load off of him as well. Plus, we were discussing billboarding vs lowpoly 3D.
.
 // CameraFacing.cs // original by Neil Carter (NCarter) // modified by Hayden Scott-Baron (Dock) - http://starfruitgames.com // allows specified orientation axis
 using UnityEngine;
 using System.Collections;
  
 public class CameraFacing : MonoBehaviour
 {
     Camera referenceCamera;
  
     public enum Axis {up, down, left, right, forward, back};
     public bool reverseFace = false; 
     public Axis axis = Axis.up; 
  
     // return a direction based upon chosen axis
     public Vector3 GetAxis (Axis refAxis)
     {
         switch (refAxis)
         {
             case Axis.down:
                 return Vector3.down; 
             case Axis.forward:
                 return Vector3.forward; 
             case Axis.back:
                 return Vector3.back; 
             case Axis.left:
                 return Vector3.left; 
             case Axis.right:
                 return Vector3.right; 
         }
  
         // default is Vector3.up
         return Vector3.up;         
     }
  
     void  Awake ()
     {
         // if no camera referenced, grab the main camera
         if (!referenceCamera)
             referenceCamera = Camera.main; 
     }
  
     void  Update ()
     {
         // rotates the object relative to the camera
         Vector3 targetPos = transform.position + referenceCamera.transform.rotation * (reverseFace ? Vector3.forward : Vector3.back) ;
         Vector3 targetOrientation = referenceCamera.transform.rotation * GetAxis(axis);
         transform.LookAt (targetPos, targetOrientation);
     }
 }
I had another 2 related questions as well.
.
Would it really be better for me/us to build a shader that did the same thing as to help with performance with 1000+ planes/quads? (More extreme scenario) .
After looking around this seems to be something that's requested fairly often, I would've thought that Unity would've introduced a simple billboarding mechanic (Unless I've missed that one, if I did, my bad.) that others could plug into for some prototyping, has anyone done this in the asset store or attempted to? 
Your answer
 
 
             Follow this Question
Related Questions
UnityEngine.UI.dll 0 Answers
visual studio tools for unity not working 1 Answer
How to use a boolean to control another object boolean using C# script 4 Answers
Could not connect to debugger 0 Answers
code in void not executing 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                