当前位置:网站首页>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 .
边栏推荐
- How to simply realize the function of menu dragging and sorting
- GBase8s如何在有外键关系的表中删除数据
- 【微信小程序开发(六)】绘制音乐播放器环形进度条
- windbg
- 数字孪生农业丨智慧农业稻米加工厂从“看天吃饭”到“知天而作”
- 修改MySQL密码的四种方法(适合初学者)
- 使用PyTorch的TensorBoard-可视化深度学习指标 | PyTorch系列(二十五)
- Center-based 3D Object Detection and Tracking(基于中心的3D目标检测和跟踪 / CenterPoint)论文笔记
- unity中物体碰撞反弹(学习)
- mysql 如图所示,现有表a,表b,需求为 通过projectcode关联a、b表,查出address不同的 idcardnum。
猜你喜欢

Pycharm 快速给整页全部相同名称修改的快捷键

Deep Residual Learning for Image Recognition浅读与实现

Redis AOF log persistence

Consolidate the data foundation in the data center
![[image defogging] image defogging based on dark channel and non-mean filtering with matlab code](/img/39/6266eb14deac9f38b7e95f7291067e.png)
[image defogging] image defogging based on dark channel and non-mean filtering with matlab code

超参数调整和实验-训练深度神经网络 | PyTorch系列(二十六)

Find - block search

程序里随处可见的interface,真的有用吗?真的用对了吗?

基于FPGA的64位8级流水线加法器

Canvas from getting started to persuading friends to give up (graphic version)
随机推荐
LoRaWAN中的网关和chirpstack到底如何通信的?UDP?GRPC?MQTT?
First knowledge of C language -- operators and keywords, define, pointer
[wechat applet development (VI)] draw the circular progress bar of the music player
Flask1.1.4 werkzeug1.0.1 source code analysis: Blueprint
[hcip] BGP features
【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码
Pytorch optimizer settings
When iPhone copies photos to the computer, the device connection often fails and the transmission is interrupted. Here's the way
Explore flex basis
Say yes, I will love you, and I will love you well
基于FPGA的64位8级流水线加法器
作业7.27 IO进程
Redis aof日志持久化
0动态规划中等 LeetCode873. 最长的斐波那契子序列的长度
mysql 如图所示,现有表a,表b,需求为 通过projectcode关联a、b表,查出address不同的 idcardnum。
Sqlserver problem solving: replication components are not installed on this server. Please run SQL Server Setup again and select the option to install replication components
[image defogging] image defogging based on dark channel and non-mean filtering with matlab code
[signal denoising] signal denoising based on Kalman filter with matlab code
【自我成长网站收集】
Manual installation of Dlib Library