当前位置:网站首页>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
}
边栏推荐
- ORM aggregate query and native database operation
- CUDA11.1在线安装
- 内核判断i2c地址上是否挂载外设
- Knowledge points of circular structure
- [leetcode] 18. Sum of four numbers
- The web project imported the MySQL driver jar package but failed to load it into the driver
- Nacos - TC Construction of High available seata (02)
- Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
- Microblogging hot search stock selection strategy
- Review of double pointer problems
猜你喜欢
随机推荐
驱动开发——第一个HelloDDK
麥斯克電子IPO被終止:曾擬募資8億 河南資產是股東
Driver development - hellowdm driver
acwing周赛58
CUDA11.1在线安装
Zynq learning notes (3) - partial reconfiguration
Flink kakfa data read and write to Hudi
GAMES202-WebGL中shader的編譯和連接(了解向)
Ora-01779: the column corresponding to the non key value saving table cannot be modified
组播和广播的知识点梳理
ORM aggregate query and native database operation
Acwing week 58
【LeetCode】18、四数之和
TCP three handshakes you need to know
图数据库ONgDB Release v-1.0.3
EditorUtility.SetDirty在Untiy中的作用以及应用
Fuzzy -- basic application method of AFL
[leetcode16] the sum of the nearest three numbers (double pointer)
yolov5 tensorrt加速
Request (request object) and response (response object)