当前位置:网站首页>Collision and rebound of objects in unity (learning)
Collision and rebound of objects in unity (learning)
2022-07-28 02:50:00 【Cangxing】
unity Object collision rebound related code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallControl : MonoBehaviour
{
// Ignore the first collision between the ball and the player
private bool isStart;
// Rigid body components
private Rigidbody2D rbody;
// The location of the last collision
private Vector3 point;
void Start()
{
rbody = GetComponent<Rigidbody2D>();
}
// Start moving
public void StartMove()
{
// Give the ball a speed Direction * Speed new Vector2(1,1).normalized( Unit vector )
rbody.velocity = new Vector2(1,1).normalized * 1;
isStart = true;
// At the beginning
point = transform.position;
}
// When colliding with walls and players
private void OnCollisionEnter2D(Collision2D collision)
{
if (isStart == false)
{
return;
}
if (transform.position == point)
{
return;
}
// Destroy bricks
if (collision.collider.tag == "brick")
{
Destroy(collision.collider.gameObject);
}
// Reflection
Vector2 inVec = transform.position - point;
point = transform.position;
Vector2 outVec = Vector2.Reflect(inVec,collision.contacts[0].normal);
rbody.velocity = outVec.normalized * 1;
}
notes 1:
collision.contacts[0]:
public ContactPoint[] contacts
Physical engine generated touchpoints . Touchpoints are stored in Collision In structure , Each contact contains a contact point 、 Normal and collision of two collision bodies
to update : It should be avoided , Because it will generate memory garbage . change to the use of sth. GetContact or GetContacts.
| Variable | effect |
|---|---|
| normal | Normal of contact point . |
| otherCollider | Other colliding bodies in contact at this point |
| point | contact point |
| separation | The distance between the collision bodies at this contact point |
| thisCollider | The first collision body contacted at this point . |
notes 2:
public static Vector2 Reflect (Vector2 inDirection, Vector2 inNormal);
effect : Reflect a vector from the vector defined by the normal .
边栏推荐
- windbg
- First knowledge of C language -- operators and keywords, define, pointer
- Canvas from getting started to persuading friends to give up (graphic version)
- 数字孪生农业丨智慧农业稻米加工厂从“看天吃饭”到“知天而作”
- unity中物体碰撞反弹(学习)
- Eigenvalues and eigenvectors
- 怎么简单实现菜单拖拽排序的功能
- Is the interface that can be seen everywhere in the program really useful? Is it really right?
- Typescript (zero) -- introduction, environment construction, first instance
- Pycharm 快速给整页全部相同名称修改的快捷键
猜你喜欢
随机推荐
TypeScript(零) —— 简介、环境搭建、第一个实例
LETV responded that employees live an immortal life without internal problems and bosses; Apple refuses to store user icloud data in Russia; Dapr 1.8.0 release | geek headlines
Center-based 3D Object Detection and Tracking(基于中心的3D目标检测和跟踪 / CenterPoint)论文笔记
JS event loop synchronous task, asynchronous task (micro task, macro task) problem analysis
Should programmers choose outsourcing companies
Maskedauutoencoders visual learner cvpr2022
Typescript (zero) -- introduction, environment construction, first instance
The virtual host website cannot access the self-test method
第二季度邮件安全报告:邮件攻击暴增4倍,利用知名品牌获取信任
How is insert locked in MySQL? (glory Collection Edition)
Is the interface that can be seen everywhere in the program really useful? Is it really right?
POC simulation attack weapon - Introduction to nucleus (I)
Job 7.27 IO process
ps 简单使用
【信号去噪】基于卡尔曼滤波实现信号去噪附matlab代码
功能测试和非功能测试区别简析,上海好口碑软件测试公司推荐
[image defogging] image defogging based on dark channel and non-mean filtering with matlab code
没法预测明天的涨跌
【LeetCode】13. Linked List Cycle·环形链表
关于Sqli-labs单引号不报错的问题









