当前位置:网站首页>In unity3d, billboard effect can be realized towards another target
In unity3d, billboard effect can be realized towards another target
2022-06-12 06:06:00 【VR technology Xiaoguang】
In yesterday's work , I want to realize the special effect of meteors crossing the sky . The face normal faces the camera and the head faces the flying target .
The idea is , It is realized by pasting a picture on the model piece and moving it . Just use the billboard . I thought it was a very simple thing ! The result is lookat function , Realization mesh Facing the camera is OK , But the head of a meteor can never face the direction of the flying target . Always fly sideways and sideways . A very simple thing took a lot of brains to come up with .
How on earth can we make the two axes of an object face different objects , It seems that only lookat No good , You still have to write your own code .
This effect is achieved in two steps :
1. Make an axis of the object face the target point .
2. Make the other axis of the object face the camera .( This step should not affect the previous step x Axis orientation )

About the first step , I refer to the content of this blog
Code up :
// toward target vector
var targetDir = looktarget.position - gameObject.transform.position;
// Specify which axis faces the target
var fromDir = gameObject.transform.right;// gameObject.transform.rotation * gameObject.transform.right;
// Calculate the vector perpendicular to the orientation axis and the target direction ( Rotation axis )
var axis = Vector3.Cross(fromDir, targetDir).normalized;
// Calculate the angle between the current direction and the target direction
var angle = Vector3.Angle(fromDir, targetDir);
// Rotate the current direction to the target direction by a certain angle , This angle value can be interpolated
gameObject.transform.rotation = Quaternion.AngleAxis(angle, axis) * gameObject.transform.rotation;About the second step
First , Calculate the camera vector at YOZ Projection in plane , Then find the projection vector and y Angle between axes , And then according to x Axis to rotate this
Angle is OK .
var camdir = cam.transform.position - gameObject.transform.position;
var touying = ProjectOnPlane(camdir, gameObject.transform.right);
var q = Quaternion.FromToRotation(gameObject.transform.up, touying);
gameObject.transform.rotation = q * gameObject.transform.rotation; // Find the projection vector of the vector on the plane
Vector3 ProjectOnPlane(Vector3 vp, Vector3 vn)
{
Vector3 vt = new Vector3();
vt = vp - vn * Vector3.Dot(vp, vn) / Vector3.Dot(vn, vn);
return vt;
}Final effect :



边栏推荐
- 项目管理与统筹
- Leetcode-1705. Maximum number of apples to eat
- dlib 人脸检测
- 姿态估计之2D人体姿态估计 - PifPaf:Composite Fields for Human Pose Estimation
- JS变量作用域
- Understand Houdini's (heightfield) remap operation
- 肝了一個月的 DDD,一文帶你掌握
- Leetcode-139. Word splitting
- 数据库为什么不使用hash表?
- C # converts the hexadecimal code form of text to text (ASCII)
猜你喜欢

数据库为什么不使用hash表?

Execute sh script to prompt "[[: not found" solution. The difference between Bash and sh

EBook upload

2D human pose estimation for pose estimation - pifpaf:composite fields for human pose estimation

Leetcode-1043. Separate arrays for maximum sum

How do I get the date and time from the Internet- How to get DateTime from the internet?

sqlite交叉编译动态库
![[untitled]](/img/75/599c5b13dd483fad50f73ddb431989.jpg)
[untitled]

Unable to access this account. You may need to update your password or grant the account permission to synchronize to this device. Tencent corporate email

(UE4 4.27) customize globalshader
随机推荐
关于 Sensor flicker/banding现象的解释
[Yu Yue education] basic reference materials of accounting of Nanjing Normal University
Leetcode-1706. Where does the club fall
项目管理与统筹
Leetcode-1512. Number of good pairs
Project and build Publishing
MySQL master-slave, 6 minutes to master
nRF52832自定義服務與特性
Idea common configuration
Nrf52832 custom services and features
Sqlite Cross - compile Dynamic Library
前台展示LED数字(计算器上数字类型)
How to increase heap size of JVM [duplicate] - how to increase heap size of JVM [duplicate]
IBL of directx11 advanced tutorial PBR (3)
Cross compile libev
基于LFFD模型目标检测自动标注生成xml文件
Divide a folder image into training set and test set
Three years of sharpening a sword: insight into the R & D efficiency of ant financial services
三年磨一剑:蚂蚁金服的研发效能洞察实践
The unity3d script searches for colliders with overlaps within the specified radius