当前位置:网站首页>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);
作用:从法线定义的向量反射一个向量。
边栏推荐
- Leetcode judge whether palindrome number
- JVM tuning -xms -xmx -xmn -xss
- 【自我成长网站收集】
- 【LeetCode】13. Linked List Cycle·环形链表
- Learn this trick and never be afraid to let the code collapse by mistake
- 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.
- QT implementation disable shortcut key
- MySQL's way to solve deadlock - lock analysis of common SQL statements
- Wechat campus bathroom reservation applet graduation design finished product (2) applet function
- Use of Day6 functions and modules
猜你喜欢

【HCIP】路由策略、策略路由
![[Yugong series] July 2022 go teaching course 019 - for circular structure](/img/40/b4e673de0462c3dd6ca8b8fb513914.png)
[Yugong series] July 2022 go teaching course 019 - for circular structure

Four methods of modifying MySQL password (suitable for beginners)

mysql 如图所示,现有表a,表b,需求为 通过projectcode关联a、b表,查出address不同的 idcardnum。

新基建助力智能化道路交通领域的转型发展
![[data processing] boxplot drawing](/img/4e/c4f863d06d8b318e6bb2d40e0c5ed3.png)
[data processing] boxplot drawing

"The faster the code is written, the slower the program runs"

Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
![[advanced ROS] Lecture 9 robot model motion based on rviz and arbotix control](/img/7f/f0360210e8a9f7e45410d79635bfd9.png)
[advanced ROS] Lecture 9 robot model motion based on rviz and arbotix control

「冒死上传」Proe/Creo产品结构设计-止口与扣位
随机推荐
Maskedauutoencoders visual learner cvpr2022
[Yugong series] use of tabby integrated terminal in July 2022
What is eplato cast by Plato farm on elephant swap?
unordered_ The hash function of map and the storage mode of hash bucket
基于FPGA的64位8级流水线加法器
【OpenGL】GLES20.glClear
作业7.27 IO进程
MySQL explain (glory Collection Edition)
「冒死上传」Proe/Creo产品结构设计-止口与扣位
Lombok prompts variable log error when using JUnit test in idea
What problems should be avoided when using BigDecimal type? (glory Collection Edition)
[hcip] BGP Foundation
2022.7.8 eth price analysis
第二季度邮件安全报告:邮件攻击暴增4倍,利用知名品牌获取信任
【英雄哥七月集训】第 27天:图
cn+dt
Emotional drama in the world Zhou Bingkun lost his job because he saw Tu Zhiqiang and was shot
【信号去噪】基于卡尔曼滤波实现信号去噪附matlab代码
Mysql Explain 详解(荣耀典藏版)
分层图解决的一些最短路问题