当前位置:网站首页>Unity Vector3. Use and calculation principle of reflect
Unity Vector3. Use and calculation principle of reflect
2022-07-06 05:13:00 【Cuijiahao】
Calculate the reflection vector that the vector projects onto the object
Collision simulation :
public class test3 : MonoBehaviour
{
Vector3 dir;
Vector3 pos;
private void Start()
{
dir = transform.right;
pos = transform.position;
}
private void Update()
{
transform.position += dir * Time.deltaTime*4;
}
private void OnCollisionEnter2D(Collision2D collision)
{
Vector2 inDirection = (transform.position - pos).normalized;
Vector2 inNormal = collision.contacts[0].normal;
dir = Vector2.Reflect(inDirection, inNormal);
pos = transform.position;
}
}
Ray simulation :
void OnDrawGizmos()
{
#if UNITY_EDITOR
Color prevColor = Gizmos.color;
// Shoot a red ray from the origin to the right
Gizmos.color = Color.red;
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.right, Mathf.Infinity);
Gizmos.DrawLine(transform.position, hitInfo.point);
// The ray touches the normal of the colliding object
Gizmos.color = Color.green;
Gizmos.DrawLine(hitInfo.point, hitInfo.normal * 0.2f);
// Reflection
Gizmos.color = Color.blue;
Vector2 direction = Vector2.Reflect(hitInfo.point - (Vector2)transform.position, hitInfo.normal);
Gizmos.DrawLine(hitInfo.point, direction * 0.1f);
Gizmos.color = prevColor;
#endif
}
边栏推荐
- Upload nestjs configuration files, configure the use of middleware and pipelines
- RT thread analysis - object container implementation and function
- JS quick start (II)
- EditorUtility.SetDirty在Untiy中的作用以及应用
- 2021 RoboCom 世界机器人开发者大赛-本科组(复赛)
- Postman管理测试用例
- Postman manage test cases
- GAMES202-WebGL中shader的編譯和連接(了解向)
- 关于Unity Inspector上的一些常用技巧,一般用于编辑器扩展或者其他
- The IPO of mesk Electronics was terminated: Henan assets, which was once intended to raise 800 million yuan, was a shareholder
猜你喜欢
yolov5 tensorrt加速
nacos-高可用seata之TC搭建(02)
The IPO of mesk Electronics was terminated: Henan assets, which was once intended to raise 800 million yuan, was a shareholder
Pix2pix: image to image conversion using conditional countermeasure networks
Rce code and Command Execution Vulnerability
[leetcode16] the sum of the nearest three numbers (double pointer)
Microblogging hot search stock selection strategy
趋势前沿 | 达摩院语音 AI 最新技术大全
idea一键导包
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
The web project imported the MySQL driver jar package but failed to load it into the driver
F12 solve the problem that web pages cannot be copied
Using stopwatch to count code time
Excel转换为Lua的配置文件
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
Postman manage test cases
麥斯克電子IPO被終止:曾擬募資8億 河南資產是股東
Oracle deletes duplicate data, leaving only one
MySQL if and ifnull use
Lepton 无损压缩原理及性能分析
Golang -- TCP implements concurrency (server and client)
RT thread analysis log system RT_ Kprintf analysis
nacos-高可用seata之TC搭建(02)
Finance online homework
Oracle query table index, unique constraint, field
Codeforces Round #804 (Div. 2)
Hyperledger Fabric2. Some basic concepts of X (1)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
GAMES202-WebGL中shader的編譯和連接(了解向)
Postman断言