unity3d Billboard
CameraFacingBillboard
CameraFacingBillboard
Author: Neil Carter (NCarter)
Contents[hide] |
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.
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.
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.
C# - CameraFacingBillboard.cs
using UnityEngine;
using System.Collections; public class CameraFacingBillboard : MonoBehaviour
{
public Camera m_Camera; void Update()
{
transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.back,
m_Camera.transform.rotation * Vector3.up);
}
}
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;
} void Update(){
if(amActive==true){
myContainer.transform.LookAt(myContainer.transform.position + m_Camera.transform.rotation * Vector3.back, m_Camera.transform.rotation * Vector3.up);
}
}
}
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;
} 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);
}
}
unity3d Billboard的更多相关文章
- 使用Unity3D的50个技巧
使用Unity3D的50个技巧 刚开始学习Unity3D时间不长,在看各种资料.除了官方的手册以外,其他人的经验也是非常有益的.偶尔看到老外这篇文章,觉得还不错,于是翻译过来和大家共享.原文地址:ht ...
- KSFramework:Unity3D开发框架快速入门
KSFramework知识 https://github.com/mr-kelly/KSFramework KSFramework是一个整合KEngine.SLua和一些开发组件组成的全功能Unity ...
- Unity3D脚本中文系列教程(十五)
http://dong2008hong.blog.163.com/blog/static/4696882720140322449780/ Unity3D脚本中文系列教程(十四) ◆ LightRend ...
- [原]Unity3D深入浅出 - 粒子系统(Particle System)
粒子系统是在三维空间渲染出来的二维图像,主要用于烟,火,水滴,落叶等效果.一个粒子系统由粒子发射器.粒子动画器和粒子渲染器三个独立的部分组成. Unity中自带了一些粒子效果,在Assets>I ...
- Unity3D用户手册
Unity Manual 用户手册 Welcome to Unity. 欢迎使用Unity. Unity is made to empower users to create the best int ...
- Unity3d操作的一些技巧知识点和BUG解决方案
自己记录一些东西,转载请良心注明出处. 1.如何同时打开两个UNITY3D项目. 有时候需要对比,或者需要添加另一个项目的某资源到目前项目,同时打开两个项目看起来会比较明了.如果直接打开的话, ...
- 使用Unity3D的50个技巧:Unity3D最佳实践
转自:http://www.tuicool.com/articles/buMz63I 刚开始学习unity3d时间不长,在看各种资料.除了官方的手册以外,其他人的经验也是非常有益的.偶尔看到老外这篇 ...
- Unity3D学习笔记——组件之Effects(效果/特效)——Particle System(粒子系统)
Effects:效果/特效. Particle System:粒子系统.可用于创建烟雾.气流.火焰.涟漪等效果. 在Unity3D 3.5版本之后退出了新的shuriken粒子系统: 添加组件之后 ...
- [Unity3D]Unity3D叙利亚NGUI血液和技能的冷却效果
---------------------------------------------------------------------------------------------------- ...
随机推荐
- drf解决跨域问题 使用 django-corse-headers扩展
跨域CORS 使用django-corse-headers扩展 安装 pip install django-cors-headers 添加应用 INSTALLED_APPS = ( ... 'cors ...
- jQuery制作table表格布局插件带有列左右拖动效果
压缩包:http://www.xwcms.net/js/bddm/99004.html
- c++知识点总结--静态与动态类型
对象的静态类型(static type),就是它在程序中被声明时所采用的类型 对象的动态类型(dynamic type)是指“目前所指对象的类型” vitual 函数是动态绑定而来,调用一个vir ...
- STL之deque使用简介
deque函数列表 函数 c.assign(beg,end)c.assign(n,elem) c.at(idx) c.back() c.begin() c.clear() deque<Elem& ...
- linux备忘录-bash
知识点 环境变量 HOME PATH MAIL SHELL RANDOM // 0~32767的随机数 declare -i number=$RANDOM*10/32768 //0-9的随机数 HIS ...
- 【操作系统】关于C语言设计程序退出自动关闭窗口的问题
有些同学在做实验一 命令解释程序的编写的时候,输入quit命令退出程序,窗口并没有关闭,如下图所示需要Press any key to continue(按任意键)之后才关闭. 出现这个结果的原因是在 ...
- Android中如何为自定义控件增加状态?
在android开发中我们常常需要对控件进行相关操作,虽然网上已有很多对控件酷炫的操作,但小编今天给大家分享的纯属实用出发.在查看了一些列安卓教程和文档后,发现了一位大牛分享的非常不错的有关andro ...
- BZOJ3622 已经没有什么好害怕的了 【dp + 二项式反演】
题目链接 BZOJ3622 题解 既已开题 那就已经没有什么好害怕的了 由题目中奇怪的条件我们可以特判掉\(n - k\)为奇数时答案为\(0\) 否则我们要求的就是糖果大于药片恰好有\(\frac{ ...
- 洛谷T8116 密码
T8116 密码 题目描述 YJC把核弹发射密码忘掉了……其实是密码被加密了,但是YJC不会解密.密码由n个数字组成,第i个数字被加密成了如下形式:第k小的满足(2^L)|(P-1)且P为质数的P.Y ...
- 当表名存在 点 的时候,EntityFramework的写法
原文发布时间为:2011-05-17 -- 来源于本人的百度文章 [由搬家工具导入] 需要在 Context 中,重写 DbContext 中的 OnModelCreating 方法 进行 mappi ...