Skip to content

Instantly share code, notes, and snippets.

@litefeel
Created March 15, 2016 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save litefeel/c56be97c203c4e08dd33 to your computer and use it in GitHub Desktop.
Save litefeel/c56be97c203c4e08dd33 to your computer and use it in GitHub Desktop.
// 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);
}
}
using UnityEngine;
using System.Collections;
public class CameraFacingBillboard : MonoBehaviour
{
public Camera m_Camera;
void Update()
{
transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.forward,
m_Camera.transform.rotation * Vector3.up);
}
}
//cameraFacingBillboard.cs v02
//by Neil Carter (NCarter)
//modified by Juan Castaneda (juanelo)
//
//added in-between GRP object to perform rotations on
//added auto-find main camera
//added un-initialized state, where script will do nothing
using UnityEngine;
using System.Collections;
public class CameraFacingBillboard2 : MonoBehaviour
{
public Camera m_Camera;
public bool amActive =false;
public bool autoInit =false;
GameObject myContainer;
void Awake(){
if (autoInit == true){
m_Camera = Camera.main;
amActive = true;
}
myContainer = new GameObject();
myContainer.name = "GRP_"+transform.gameObject.name;
myContainer.transform.position = transform.position;
transform.parent = myContainer.transform;
}
void Update(){
if(amActive==true){
myContainer.transform.LookAt(myContainer.transform.position + m_Camera.transform.rotation * Vector3.back, m_Camera.transform.rotation * Vector3.up);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment