当前位置:网站首页>unity中物体碰撞反弹(学习)
unity中物体碰撞反弹(学习)
2022-07-28 01:32:00 【沧行】
unity中物体碰撞反弹相关代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallControl : MonoBehaviour
{
//忽略小球第一次与玩家的碰撞
private bool isStart;
//刚体组件
private Rigidbody2D rbody;
//上次碰撞的位置
private Vector3 point;
void Start()
{
rbody = GetComponent<Rigidbody2D>();
}
//开始移动
public void StartMove()
{
//给球一个速度 方向 * 速度 new Vector2(1,1).normalized(单位向量)
rbody.velocity = new Vector2(1,1).normalized * 1;
isStart = true;
//最开始的位置
point = transform.position;
}
//当与墙壁和玩家碰撞时
private void OnCollisionEnter2D(Collision2D collision)
{
if (isStart == false)
{
return;
}
if (transform.position == point)
{
return;
}
//销毁砖块
if (collision.collider.tag == "brick")
{
Destroy(collision.collider.gameObject);
}
//反射
Vector2 inVec = transform.position - point;
point = transform.position;
Vector2 outVec = Vector2.Reflect(inVec,collision.contacts[0].normal);
rbody.velocity = outVec.normalized * 1;
}
注1:
collision.contacts[0]:
public ContactPoint[] contacts
物理引擎生成的接触点。接触点存储在 Collision 结构中,每个接触均包含一个接触点、法线和碰撞的两个碰撞体
更新:应避免使用它,因为它会产生内存垃圾。改用 GetContact 或 GetContacts。
| 变量 | 作用 |
|---|---|
| normal | 接触点的法线。 |
| otherCollider | 在该点接触的其他碰撞体 |
| point | 接触点 |
| separation | 在该接触点的碰撞体之间的距离 |
| thisCollider | 在该点接触的第一个碰撞体。 |
注2:
public static Vector2 Reflect (Vector2 inDirection, Vector2 inNormal);
作用:从法线定义的向量反射一个向量。
边栏推荐
- Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
- Wechat campus bathroom reservation applet graduation design finished product (3) background function
- Hardware standard
- what‘s the meaning of “rc“ in release name
- regular expression
- [data processing] boxplot drawing
- Soft test - database (2) relational model
- 基于stm32的恒功率无线充电
- MySQL is shown in the figure. The existing tables a and B need to be associated with a and B tables through projectcode to find idcardnum with different addresses.
- 关于Sqli-labs单引号不报错的问题
猜你喜欢

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

并发编程的三大核心问题(荣耀典藏版)

retainface使用报错:ModuleNotFoundError: No module named 'rcnn.cython.bbox'

Newline required at end of file but not found.

程序里随处可见的interface,真的有用吗?真的用对了吗?
![[self growth website collection]](/img/42/fa17c9167697543defd3e63a97237a.png)
[self growth website collection]

Use try-with-resources or close this
![This operation may not be worth money, but it is worth learning | [batch cutting of pictures]](/img/e8/a34e471b0089f8085b140c74b5c01f.jpg)
This operation may not be worth money, but it is worth learning | [batch cutting of pictures]

Chapter 3 business function development (batch export of market activities, Apache POI)

MySQL是如何利用索引的(荣耀典藏版)
随机推荐
Leetcode judge whether palindrome number
Eredi reappeared at the digital China Summit and continued to deepen the protection of green waters and mountains with science and technology
[self growth website collection]
QT implementation disable shortcut key
获取两个集合相差数据
In practical work, how do I use postman for interface testing?
Wechat campus bathroom reservation applet graduation design finished product (1) development outline
POC模拟攻击利器 —— Nuclei入门(一)
"Risking your life to upload" proe/creo product structure design - seam and buckle
Chapter 3 business function development (batch export of market activities, Apache POI)
【TA-霜狼_may-《百人计划》】图形3.5 Early-z 和 Z-prepass
Email security report in the second quarter: email attacks have soared fourfold, and well-known brands have been used to gain trust
Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
1313_pyserial的安装以及文档的生成
[leetcode] 13. linked list cycle · circular linked list
JVM tuning -xms -xmx -xmn -xss
Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
支付宝小程序授权/获取用户信息
mysql: error while loading shared libraries: libtinfo.so. 5 solutions
[advanced ROS] Lecture 9 robot model motion based on rviz and arbotix control