CameraFacingBillboard

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Alternative Mod)
 
(20 intermediate revisions by 8 users not shown)
Line 3: Line 3:
 
[[Category: C Sharp]]
 
[[Category: C Sharp]]
 
[[Category: Billboard]]
 
[[Category: Billboard]]
 +
[[Category: Mods]]
 
Author: Neil Carter (NCarter)
 
Author: Neil Carter (NCarter)
 
==Description==
 
==Description==
Line 16: Line 17:
  
 
==C# - CameraFacingBillboard.cs==
 
==C# - CameraFacingBillboard.cs==
<csharp>using UnityEngine;
+
<syntaxhighlight lang="csharp">using UnityEngine;
 
using System.Collections;
 
using System.Collections;
  
 
public class CameraFacingBillboard : MonoBehaviour
 
public class CameraFacingBillboard : MonoBehaviour
 
{
 
{
public Camera m_Camera;
+
    public Camera m_Camera;
  
void Update()
+
    //Orient the camera after all movement is completed this frame to avoid jittering
{
+
    void LateUpdate()
transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.back,
+
    {
m_Camera.transform.rotation * Vector3.up);
+
        transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.forward,
}
+
            m_Camera.transform.rotation * Vector3.up);
 +
    }
 
}
 
}
</csharp>
+
</syntaxhighlight>
  
 +
==Mods==
 +
This Mod will additionally:<br>
 +
-Find the default camera in the scene<br>
 +
-Create an empty "container" GameObject as parent of the billboard, and will rotate this object instead. This allows the user to assign a predefined rotation to the billboard object.<br>
 +
-Require initialization. (just set "autoInit" to "true")<br>
 +
Mod Author: juanelo<br>
  
.
+
<syntaxhighlight lang="csharp">//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 CameraFacingBillboard : 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;
 +
}
  
 +
    //Orient the camera after all movement is completed this frame to avoid jittering
 +
    void LateUpdate(){
 +
        if(amActive==true){
 +
        myContainer.transform.LookAt(myContainer.transform.position + m_Camera.transform.rotation * Vector3.back, m_Camera.transform.rotation * Vector3.up);
 +
    }
 +
    }
 +
}</syntaxhighlight>
  
 +
==Alternative Mod==
 +
This Mod will:<br>
 +
- Find the default camera in the scene<br>
 +
- Allow default axis to be specified<br>
 +
Mod Author: Hayden Scott-Baron (dock)<br>
  
 +
<syntaxhighlight lang="csharp">// 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:
<div style="overflow:auto;height:1px;">
+
return Vector3.back;
Excuse for my post but I do not have money to buy meal to my children. Forgive me please.
+
case Axis.left:
[http://47964.rapidforum.com pharmacy]
+
return Vector3.left;
[http://blog.myspace.com/cheap_phentermine pharmacy]
+
case Axis.right:
[http://1.myfreebulletinboard.com/phentermine/ pharmacy]
+
return Vector3.right;
[http://i.mobb.ru/dir/phentermine/index.htm pharmacy]
+
}
[http://www.viagrastories.com/buy.shtml pharmacy]
+
[http://www.multiweb.cz/buy_online/viagra.html  pharmacy]
+
// default is Vector3.up
[http://buy-valium-online.inknoise.com/cheap pharmacy]
+
return Vector3.up;
[http://www.republika.pl/nashdor/buy-valium pharmacy]
+
}
[http://eteamz.active.com/tramadolhcl/files/buy-valium.html  pharmacy]
+
[http://mulder.blogspirit.com/files/valium.html  pharmacy]
+
void  Awake ()
[http://www.birding.com/forums/showflat.php?Number=2099 pharmacy]
+
{
[http://www.ultraguest.com/?id=1141564200 pharmacy]
+
// if no camera referenced, grab the main camera
[http://eteamz.active.com/cheap-fioricet-online pharmacy]
+
if (!referenceCamera)
[http://fioricet.inknoise.com/fioricet pharmacy]
+
referenceCamera = Camera.main;
[http://www.republika.pl/buyonline2006/ fioricet/buy-fioricet.html  pharmacy]
+
}
[http://pub33.bravenet.com/forum/2761181745 pharmacy]
+
        //Orient the camera after all movement is completed this frame to avoid jittering
[http://www.financialexpress.com/ adlinks/Fioricet/buy_fioricet.htm pharmacy]
+
void LateUpdate ()
[http://drug.inknoise.com/diazepam pharmacy]
+
{
[http://xph8buydiazepam.proboards57.com pharmacy]
+
// rotates the object relative to the camera
[http://www.upsaid.com/buydiazepam pharmacy]
+
Vector3 targetPos = transform.position + referenceCamera.transform.rotation * (reverseFace ? Vector3.forward : Vector3.back) ;
[http://eteamz.active.com/diazepams pharmacy]
+
Vector3 targetOrientation = referenceCamera.transform.rotation * GetAxis(axis);
[http://diazepams.proboards52.com pharmacy]
+
transform.LookAt (targetPos, targetOrientation);
[http://pub41.bravenet.com/forum/3495470951 pharmacy]
+
}
[http://myblog.de/dronnoal pharmacy]
+
}</syntaxhighlight>
[http://spaces.msn.com/buy-cheap-diazepam pharmacy]
+
[http://usmo4.discoverlife.org/mp/20q?go=http://cyberimagegh.com/1/renova.html renova]
+
[http://www.ep.u-tokai.ac.jp/seido/hp.cgi?http://bestsearch20.com/1/wellbutrin.html wellbutrin]
+
[http://usmo4.discoverlife.org/mp/20q?go=http://bestsearch20.com/1/claritin.html claritin]
+
[http://libweb5.princeton.edu/scripts/link/access.pl?resource=ejournal&title=BMJ&type=Web&url=http://bestsearch20.com/1/zyrtec.html zyrtec]
+
[http://www.liverpoolfc.tv/lfctvt/?cid=36&inv=75&dest=http://bestsearch20.com/1/zoloft.html zoloft]
+
[http://libweb5.princeton.edu/scripts/link/access.pl?resource=ejournal&title=BMJ&type=Web&url=http://bestsearch20.com/1/prilosec.html prilosec]
+
[http://www.fort-myers-florida-real-estate.com/include.php?URL=http://cyberimagegh.com/1/valtrex.html valtrex]
+
[http://www.oas.org/main/main.asp?sLang=E&sLink=http://cyberimagegh.com/1/prozac.html prozac]
+
[http://www.udayton.edu/~vlc/misc/external_frame.php?lang=en&url=http://cyberimagegh.com/1/imitrex.html imitrex]
+
[http://www.udayton.edu/~vlc/misc/external_frame.php?lang=en&url=http://cyberimagegh.com/1/buspar.html buspar]
+
[http://libweb5.princeton.edu/scripts/link/access.pl?resource=ejournal&title=BMJ&type=Web&url=http://bestsearch20.com/1/valtrex.html valtrex]
+
[http://www.liverpoolfc.tv/lfctvt/?cid=36&inv=75&dest=http://bestsearch20.com/1/tadalafil.html tadalafil]
+
[http://www.oas.org/main/main.asp?sLang=E&sLink=http://cyberimagegh.com/1/nexium.html nexium]
+
[http://libweb5.princeton.edu/scripts/link/access.pl?resource=ejournal&title=BMJ&type=Web&url=http://bestsearch20.com/1/retin.html retin]
+
[http://www.oas.org/main/main.asp?sLang=E&sLink=http://bestsearch20.com/1/celexa.html celexa]
+
[http://usmo4.discoverlife.org/mp/20q?go=http://bestsearch20.com/1/buspar.html buspar]
+
[http://usmo4.discoverlife.org/mp/20q?go=http://bestsearch20.com/1/imitrex.html imitrex]
+
[http://rwr.ru/exploder/?url=http://cyberimagegh.com/1/acyclovir.html acyclovir]
+
[http://rwr.ru/exploder/?url=http://cyberimagegh.com/1/flonase.html flonase]
+
[http://rwr.ru/exploder/?url=http://cyberimagegh.com/1/lexapro.html lexapro]
+
[http://www.oas.org/main/main.asp?sLang=E&sLink=http://cyberimagegh.com/1/sildenafil.html sildenafil]
+
[http://www.fort-myers-florida-real-estate.com/include.php?URL=http://cyberimagegh.com/1/yasmin.html yasmin]
+
[http://usmo4.discoverlife.org/mp/20q?go=http://cyberimagegh.com/1/zoloft.html zoloft]
+
[http://usmo4.discoverlife.org/mp/20q?go=http://cyberimagegh.com/1/paxil.html paxil]
+
[http://h1.ripway.com/redka/what_does_valium_look_like.html what does valium look like]
+
[http://www.net4free.org/Health/redkas/3d_genital_valium.html 3d genital valium]
+
[http://www.net4free.org/Health/redkas/affect_side_valium.html affect side valium]
+
[http://www.net4free.org/Health/redkas/as_as_good_valerian_valium.html as as good valerian valium]
+
[http://www.net4free.org/Health/redkas/buy_cheap_valium.html buy cheap valium]
+
[http://www.net4free.org/Health/redkas/buy_valium.html buy valium]
+
[http://www.net4free.org/Health/redkas/buy_valium_diazepam.html buy valium diazepam]
+
[http://www.net4free.org/Health/redkas/buy_valium_online.html buy valium online]
+
[http://www.net4free.org/Health/redkas/buy_valium_online_without_a_prescription.html buy valium online without a prescription]
+
[http://www.net4free.org/Health/redkas/buy_valium_without_prescription.html buy valium without prescription]
+
[http://www.net4free.org/Health/redkas/buying_valium.html buying valium]
+
[http://www.net4free.org/Health/redkas/canada_valium.html canada valium]
+
[http://www.net4free.org/Health/redkas/canadian_online_pharmacy_valium.html canadian online pharmacy valium]
+
[http://www.net4free.org/Health/redkas/cat_valium.html cat valium]
+
[http://www.net4free.org/Health/redkas/cheap_valium.html cheap valium]
+
[http://www.net4free.org/Health/redkas/cheapest_valium.html cheapest valium]
+
[http://www.net4free.org/Health/redkas/description_valium.html description valium]
+
[http://www.net4free.org/Health/redkas/discount_valium.html discount valium]
+
[http://www.net4free.org/Health/redkas/dj_valium.html dj valium]
+
[http://www.net4free.org/Health/redkas/dosages_valium.html dosages valium]
+
[http://www.net4free.org/Health/redkas/drug_testing_for_valium.html drug testing for valium]
+
[http://www.net4free.org/Health/redkas/drug_valium.html drug valium]
+
[http://www.net4free.org/Health/redkas/effects_forum_valium.html effects forum valium]
+
[http://www.net4free.org/Health/redkas/fda_valium.html fda valium]
+
[http://www.net4free.org/Health/redkas/free_consultation_valium.html free consultation valium]
+
[http://www.net4free.org/Health/redkas/generic_valium.html generic valium]
+
[http://www.net4free.org/Health/redkas/generic_valium_picture.html generic valium picture]
+
[http://www.net4free.org/Health/redkas/herbal_valium.html herbal valium]
+
[http://www.net4free.org/Health/redkas/history_of_valium.html history of valium]
+
[http://www.net4free.org/Health/redkas/how_long_does_valium_stay_in_your_system.html how long does valium stay in your system]
+
[http://www.net4free.org/Health/redkas/injecting_valium.html injecting valium]
+
[http://www.net4free.org/Health/redkas/international_pharmacy_online_valium.html international pharmacy online valium]
+
[http://www.net4free.org/Health/redkas/mexican_valium.html mexican valium]
+
[http://www.net4free.org/Health/redkas/mortgage_valium.html mortgage valium]
+
[http://www.net4free.org/Health/redkas/mylan_valium.html mylan valium]
+
[http://www.net4free.org/Health/redkas/online_pharmacy_valium.html online pharmacy valium]
+
[http://www.net4free.org/Health/redkas/online_prescription_valium.html online prescription valium]
+
[http://www.net4free.org/Health/redkas/order_valium.html order valium]
+
[http://www.net4free.org/Health/redkas/order_valium_line.html order valium line]
+
[http://www.net4free.org/Health/redkas/order_valium_online.html order valium online]
+
[http://www.net4free.org/Health/redkas/picture_pill_valium.html picture pill valium]
+
[http://www.net4free.org/Health/redkas/poker_valium.html poker valium]
+
[http://www.net4free.org/Health/redkas/prescription_valium.html prescription valium]
+
[http://www.net4free.org/Health/redkas/purchase_valium.html purchase valium]
+
[http://www.net4free.org/Health/redkas/purchase_valium_online.html purchase valium online]
+
[http://www.net4free.org/Health/redkas/roche_valium.html roche valium]
+
[http://www.net4free.org/Health/redkas/rosario_castellanos_valium_10.html rosario castellanos valium 10]
+
[http://www.net4free.org/Health/redkas/snorting_valium.html snorting valium]
+
[http://www.net4free.org/Health/redkas/symptom_valium_withdrawal.html symptom valium withdrawal]
+
[http://www.net4free.org/Health/redkas/valium.html valium]
+
[http://www.net4free.org/Health/redkas/valium_10mg.html valium 10mg]
+
[http://www.net4free.org/Health/redkas/valium_5mg.html valium 5mg]
+
[http://www.net4free.org/Health/redkas/valium_abuse.html valium abuse]
+
[http://www.net4free.org/Health/redkas/valium_addiction.html valium addiction]
+
[http://www.net4free.org/Health/redkas/valium_alternative.html valium alternative]
+
[http://www.net4free.org/Health/redkas/valium_and_alcohol.html valium and alcohol]
+
[http://www.net4free.org/Health/redkas/valium_buying_online.html valium buying online]
+
[http://www.net4free.org/Health/redkas/valium_cod.html valium cod]
+
[http://www.net4free.org/Health/redkas/valium_definition.html valium definition]
+
[http://www.net4free.org/Health/redkas/valium_diazepam.html valium diazepam]
+
[http://www.net4free.org/Health/redkas/valium_dosage.html valium dosage]
+
[http://www.net4free.org/Health/redkas/valium_dose.html valium dose]
+
[http://www.net4free.org/Health/redkas/valium_drug_test.html valium drug test]
+
[http://www.net4free.org/Health/redkas/valium_effects.html valium effects]
+
[http://www.net4free.org/Health/redkas/valium_fact.html valium fact]
+
[http://www.net4free.org/Health/redkas/valium_for_dog.html valium for dog]
+
[http://www.net4free.org/Health/redkas/valium_forum.html valium forum]
+
[http://www.net4free.org/Health/redkas/valium_half_life.html valium half life]
+
[http://www.net4free.org/Health/redkas/valium_info.html valium info]
+
[http://www.net4free.org/Health/redkas/valium_information.html valium information]
+
[http://www.net4free.org/Health/redkas/valium_manufacturer.html valium manufacturer]
+
[http://www.net4free.org/Health/redkas/valium_medication.html valium medication]
+
[http://www.net4free.org/Health/redkas/valium_mexico.html valium mexico]
+
[http://www.net4free.org/Health/redkas/valium_on_line.html valium on line]
+
[http://www.net4free.org/Health/redkas/valium_online.html valium online]
+
[http://www.net4free.org/Health/redkas/valium_overdose.html valium overdose]
+
[http://www.net4free.org/Health/redkas/valium_overnight.html valium overnight]
+
[http://www.net4free.org/Health/redkas/valium_overnight_no_prescription.html valium overnight no prescription]
+
[http://www.net4free.org/Health/redkas/valium_pharmacy.html valium pharmacy]
+
[http://www.net4free.org/Health/redkas/valium_photo.html valium photo]
+
[http://www.net4free.org/Health/redkas/valium_pic.html valium pic]
+
[http://www.net4free.org/Health/redkas/valium_picture.html valium picture]
+
[http://www.net4free.org/Health/redkas/valium_pill.html valium pill]
+
[http://www.net4free.org/Health/redkas/valium_pregnancy.html valium pregnancy]
+
[http://www.net4free.org/Health/redkas/valium_sale.html valium sale]
+
[http://www.net4free.org/Health/redkas/valium_side_effects.html valium side effects]
+
[http://www.net4free.org/Health/redkas/valium_tablet.html valium tablet]
+
[http://www.net4free.org/Health/redkas/valium_taper.html valium taper]
+
[http://www.net4free.org/Health/redkas/valium_use.html valium use]
+
[http://www.net4free.org/Health/redkas/valium_used_for.html valium used for]
+
[http://www.net4free.org/Health/redkas/valium_vicodin.html valium vicodin]
+
[http://www.net4free.org/Health/redkas/valium_vs_xanax.html valium vs xanax]
+
[http://www.net4free.org/Health/redkas/valium_web.com.html valium web.com]
+
[http://www.net4free.org/Health/redkas/valium_withdrawal.html valium withdrawal]
+
[http://www.net4free.org/Health/redkas/valium_withdrawl.html valium withdrawl]
+
[http://www.net4free.org/Health/redkas/valium_without_prescription.html valium without prescription]
+
[http://www.net4free.org/Health/redkas/what_does_valium_look_like.html what does valium look like]
+
</div>
+

Latest revision as of 17:30, 15 September 2018

Author: Neil Carter (NCarter)

Contents

[edit] Description

This script makes the object which it is attached to align itself with the camera. This is useful for billboards which should always face the camera and be the same way up as it is.

[edit] Usage

Place this script on a GameObject that you want to face the camera. Then, with the object selected, use the inspector to select the Camera you want the object to face.

You might want to change Vector3.back to Vector3.front, depending on the initial orientation of your object.

[edit] Technical Discussion

Note that the script doesn't simply point the object at the camera. Instead, it makes the object point in the same direction as the camera's forward axis (that is, the direction the camera is looking in). This might seem intuitively wrong, but it's actually correct for the one-point-perspective world of realtime computer graphics.

[edit] C# - CameraFacingBillboard.cs

using UnityEngine;
using System.Collections;
 
public class CameraFacingBillboard : MonoBehaviour
{
    public Camera m_Camera;
 
    //Orient the camera after all movement is completed this frame to avoid jittering
    void LateUpdate()
    {
        transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.forward,
            m_Camera.transform.rotation * Vector3.up);
    }
}

[edit] Mods

This Mod will additionally:
-Find the default camera in the scene
-Create an empty "container" GameObject as parent of the billboard, and will rotate this object instead. This allows the user to assign a predefined rotation to the billboard object.
-Require initialization. (just set "autoInit" to "true")
Mod Author: juanelo

//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 CameraFacingBillboard : 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;
	}
 
    //Orient the camera after all movement is completed this frame to avoid jittering
    void LateUpdate(){
        if(amActive==true){
        	myContainer.transform.LookAt(myContainer.transform.position + m_Camera.transform.rotation * Vector3.back, m_Camera.transform.rotation * Vector3.up);
	    }
    }
}

[edit] Alternative Mod

This Mod will:
- Find the default camera in the scene
- Allow default axis to be specified
Mod Author: Hayden Scott-Baron (dock)

//	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; 
	}
        //Orient the camera after all movement is completed this frame to avoid jittering
	void LateUpdate ()
	{
		// 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);
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox