Created
March 15, 2016 05:32
-
-
Save litefeel/c56be97c203c4e08dd33 to your computer and use it in GitHub Desktop.
CameraFacingBillboard http://wiki.unity3d.com/index.php/CameraFacingBillboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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