CameraFacingBillboard

 

CameraFacingBillboard

From Unify Community Wiki

 

Jump to: navigation, search

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

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraFacingBillboard : MonoBehaviour
  5. {
  6. public Camera m_Camera;
  7.  
  8. void Update()
  9. {
  10. transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.back,
  11. m_Camera.transform.rotation * Vector3.up);
  12. }
  13. }

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

  1. //cameraFacingBillboard.cs v02
  2. //by Neil Carter (NCarter)
  3. //modified by Juan Castaneda (juanelo)
  4. //
  5. //added in-between GRP object to perform rotations on
  6. //added auto-find main camera
  7. //added un-initialized state, where script will do nothing
  8. using UnityEngine;
  9. using System.Collections;
  10.  
  11. public class CameraFacingBillboard : MonoBehaviour
  12. {
  13.  
  14. public Camera m_Camera;
  15. public bool amActive =false;
  16. public bool autoInit =false;
  17. GameObject myContainer;
  18.  
  19. void Awake(){
  20. if (autoInit == true){
  21. m_Camera = Camera.main;
  22. amActive = true;
  23. }
  24.  
  25. myContainer = new GameObject();
  26. myContainer.name = "GRP_"+transform.gameObject.name;
  27. myContainer.transform.position = transform.position;
  28. transform.parent = myContainer.transform;
  29. }
  30.  
  31. void Update(){
  32. if(amActive==true){
  33. myContainer.transform.LookAt(myContainer.transform.position + m_Camera.transform.rotation * Vector3.back, m_Camera.transform.rotation * Vector3.up);
  34. }
  35. }
  36. }

Alternative Mod

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

  1. // CameraFacing.cs
  2. // original by Neil Carter (NCarter)
  3. // modified by Hayden Scott-Baron (Dock) - http://starfruitgames.com
  4. // allows specified orientation axis
  5.  
  6. using UnityEngine;
  7. using System.Collections;
  8.  
  9. public class CameraFacing : MonoBehaviour
  10. {
  11. Camera referenceCamera;
  12.  
  13. public enum Axis {up, down, left, right, forward, back};
  14. public bool reverseFace = false;
  15. public Axis axis = Axis.up;
  16.  
  17. // return a direction based upon chosen axis
  18. public Vector3 GetAxis (Axis refAxis)
  19. {
  20. switch (refAxis)
  21. {
  22. case Axis.down:
  23. return Vector3.down;
  24. case Axis.forward:
  25. return Vector3.forward;
  26. case Axis.back:
  27. return Vector3.back;
  28. case Axis.left:
  29. return Vector3.left;
  30. case Axis.right:
  31. return Vector3.right;
  32. }
  33.  
  34. // default is Vector3.up
  35. return Vector3.up;
  36. }
  37.  
  38. void Awake ()
  39. {
  40. // if no camera referenced, grab the main camera
  41. if (!referenceCamera)
  42. referenceCamera = Camera.main;
  43. }
  44.  
  45. void Update ()
  46. {
  47. // rotates the object relative to the camera
  48. Vector3 targetPos = transform.position + referenceCamera.transform.rotation * (reverseFace ? Vector3.forward : Vector3.back) ;
  49. Vector3 targetOrientation = referenceCamera.transform.rotation * GetAxis(axis);
  50. transform.LookAt (targetPos, targetOrientation);
  51. }
  52. }

unity3d Billboard的更多相关文章

  1. 使用Unity3D的50个技巧

    使用Unity3D的50个技巧 刚开始学习Unity3D时间不长,在看各种资料.除了官方的手册以外,其他人的经验也是非常有益的.偶尔看到老外这篇文章,觉得还不错,于是翻译过来和大家共享.原文地址:ht ...

  2. KSFramework:Unity3D开发框架快速入门

    KSFramework知识 https://github.com/mr-kelly/KSFramework KSFramework是一个整合KEngine.SLua和一些开发组件组成的全功能Unity ...

  3. Unity3D脚本中文系列教程(十五)

    http://dong2008hong.blog.163.com/blog/static/4696882720140322449780/ Unity3D脚本中文系列教程(十四) ◆ LightRend ...

  4. [原]Unity3D深入浅出 - 粒子系统(Particle System)

    粒子系统是在三维空间渲染出来的二维图像,主要用于烟,火,水滴,落叶等效果.一个粒子系统由粒子发射器.粒子动画器和粒子渲染器三个独立的部分组成. Unity中自带了一些粒子效果,在Assets>I ...

  5. Unity3D用户手册

    Unity Manual 用户手册 Welcome to Unity. 欢迎使用Unity. Unity is made to empower users to create the best int ...

  6. Unity3d操作的一些技巧知识点和BUG解决方案

    自己记录一些东西,转载请良心注明出处.     1.如何同时打开两个UNITY3D项目. 有时候需要对比,或者需要添加另一个项目的某资源到目前项目,同时打开两个项目看起来会比较明了.如果直接打开的话, ...

  7. 使用Unity3D的50个技巧:Unity3D最佳实践

    转自:http://www.tuicool.com/articles/buMz63I  刚开始学习unity3d时间不长,在看各种资料.除了官方的手册以外,其他人的经验也是非常有益的.偶尔看到老外这篇 ...

  8. Unity3D学习笔记——组件之Effects(效果/特效)——Particle System(粒子系统)

    Effects:效果/特效. Particle System:粒子系统.可用于创建烟雾.气流.火焰.涟漪等效果. 在Unity3D 3.5版本之后退出了新的shuriken粒子系统:   添加组件之后 ...

  9. [Unity3D]Unity3D叙利亚NGUI血液和技能的冷却效果

    ---------------------------------------------------------------------------------------------------- ...

随机推荐

  1. leetcode 174. 地下城游戏 解题报告

    leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下 ...

  2. mongo数据库 启动报错

    报错信息如下: [root@166 bin]# mongoMongoDB shell version v3.4.6-22-ga109a23connecting to: mongodb://127.0. ...

  3. GCC特性之__init修饰解析 - kasalyn的专栏 - 博客频道 - CSDN.NET

    , GCC特性之__init修饰解析 - kasalyn的专栏 - 博客频道 - CSDN.NET.MathJax_Hover_Frame {border-radius: .25em; -webkit ...

  4. SQLServer对视图或函数’XXX’的更新或插入失败,因其包含派生域或常量域解决

    原因:视图view不允许修改. 解决:重新创建一个相同结构内容的表. 解释:因为所创建的视图对其属性值进行了计算的其他形式上的改变,而对视图的更改最终表现为对表的更改而表中不存在视图的某一属性,或属性 ...

  5. vue & lifecycle methods & this bug & ES6 Arrow function & this bind bug

    vue & lifecycle methods & this bug ES6 Arrow function & this bind bug bad fetchTableData ...

  6. P3153 [CQOI2009]跳舞

    题目描述 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会”单向喜欢“) ...

  7. 【BestCoder #44】

    因为这场比赛,我愉快地逃掉了晚自修. T1一开始各种SillyB,忘了40%的最低限制... T2各种想吐槽... 明明OJ警告说%lld是不行的我就换成%I64D(上面写这样的)... 结果各种WA ...

  8. 洛谷 P4883 mzf的考验 解题报告

    P4883 mzf的考验 题目背景 \(mzf\)立志要成为一个豪杰,当然,他也是一个\(OIer\). 他希望自己除了会\(OI\)之外还会各种东西,比如心理学.吉他.把妹等等. 为了让自己有更大的 ...

  9. 使用redis实现简单的锁机制

    在测试第三方账号注册时,授权拉取后,如果两台手册同时点击注册按钮,数据库中就会新增两天一模一样的数据,而我们的需求是一个第三方账号只能绑定一个账号,所以,由此现象可以知道,这里产生了并发访问,我们应该 ...

  10. 怎么用tomcat对socket接口的程序进行调试

    对socket(套接字)的demo方法大多都是用main方法进行测试的,那如何用tomcat进行测试呢? 这里需要借助一个工具:工具的名字是:Sockettool.exe .下图是该工具的内容.连上监 ...