当前位置:网站首页>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 :



边栏推荐
- Houdini terrain creation
- 前台展示LED数字(计算器上数字类型)
- (UE4 4.27) add globalshder to the plug-in
- Research Report on water sports shoes industry - market status analysis and development prospect forecast
- Poisson disk sampling for procedural placement
- Directx11 advanced tutorial cluster based deffered shading
- Storing texture2d to hard disk JPG file with script under unity3d
- IBL of directx11 advanced tutorial PBR (3)
- The unity3d script searches for colliders with overlaps within the specified radius
- 肝了一个月的 DDD,一文带你掌握
猜你喜欢
随机推荐
(UE4 4.27) add globalshder to the plug-in
Project management and overall planning
How do I get the date and time from the Internet- How to get DateTime from the internet?
Findasync and include LINQ statements - findasync and include LINQ statements
哈工大信息内容安全实验
Types, functions and applications of intelligent sensors
SQLite cross compile dynamic library
Cross compile libev
Leetcode-1043. Separate arrays for maximum sum
cv2.fillPoly coco annotator segment坐标转化为mask图像
肝了一個月的 DDD,一文帶你掌握
[untitled]
Unity implements smooth interpolation
[C language basics] macro definition usage
Brief introduction to project development process
Simple spiral ladder generation for Houdini program modeling
2D human pose estimation for pose estimation - pifpaf:composite fields for human pose estimation
Nrf52832 -- official routine ble_ app_ UART adds the LED feature to enable the computer UART and mobile app to control the LED on and off of the development board
Poisson disk sampling for procedural placement
软件项目架构简单总结




![[PowerShell] command line output and adding system environment variables](/img/49/b92175181aa4a3fddfa3adcacf1d72.jpg)



