当前位置:网站首页>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
}
边栏推荐
- nacos-高可用seata之TC搭建(02)
- [leetcode16] the sum of the nearest three numbers (double pointer)
- yolov5 tensorrt加速
- Set detailed map + interview questions
- 關於Unity Inspector上的一些常用技巧,一般用於編輯器擴展或者其他
- ISP learning (2)
- Fuzzy -- basic application method of AFL
- Nacos TC setup of highly available Seata (02)
- Postman test report
- Orm-f & Q object
猜你喜欢
nacos-高可用seata之TC搭建(02)
Using stopwatch to count code time
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
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
Implementing fuzzy query with dataframe
RTP gb28181 document testing tool
从0到1建设智能灰度数据体系:以vivo游戏中心为例
RT thread analysis - object container implementation and function
Fiddler installed the certificate, or prompted that the certificate is invalid
Talking about the type and function of lens filter
随机推荐
GAMES202-WebGL中shader的编译和连接(了解向)
Weng Kai C language third week 3.1 punch in
Postman测试报告
February 12 relativelayout
[lgr-109] Luogu may race II & windy round 6
nacos-高可用seata之TC搭建(02)
Postman管理测试用例
Project manager, can you draw prototypes? Does the project manager need to do product design?
[buuctf.reverse] 159_ [watevrCTF 2019]Watshell
Using stopwatch to count code time
Driver development - hellowdm driver
ORM aggregate query and native database operation
Fiddler installed the certificate, or prompted that the certificate is invalid
Crazy God said redis notes
[leetcode16] the sum of the nearest three numbers (double pointer)
饼干(考试版)
Compilation et connexion de shader dans games202 - webgl (comprendre la direction)
Postman assertion
[NOIP2008 提高组] 笨小猴
关于es8316的音频爆破音的解决