当前位置:网站首页>Unity 绘制弹球和台球的运动轨迹
Unity 绘制弹球和台球的运动轨迹
2022-07-04 03:47:00 【Cuijiahao】
效果图
废话不多说,直接上效果图

原理
原理就是创建一个 轨迹球,在第二场景中模拟白球的物理运动轨迹,并记录轨迹! 也就是创建一个不可见的场景,用来模拟球的运动轨迹绘制。
碰撞后,被碰撞物体的运动轨迹绘制 将动量传递给碰撞对象,得到碰撞的对象。 从而记录被碰撞对象的运动轨迹
场景
Physics为物理场景,GoBallList用来存放要反射的小球,TrackBallTransform为要模拟的轨迹球

代码
有两个关键的代码:
调用 physicsScene.Simulate(0.02f);完成轨迹预测;
这个API 需要 Physics.autoSimulation = false;//关闭 物理模拟
具体代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using Lean.Touch;
using DG.Tweening;
using UnityEngine.SceneManagement;
public class BallPool : MonoBehaviour
{
public List<GameObject> GoBallList = new List<GameObject>();//所有的反射小球
public Transform trackBallTransform;//轨迹球(即需要物理模拟的小球)
private Rigidbody2D trackBallRigidbody;//轨迹球的刚体
private PhysicsScene2D physicsScene; //物理场景
public Vector3 vecBallPoint;//轨迹球的位置
private void Start()
{
CreateBallReflect();//创建反射小球
CreatePhysicsScene();//创建一个用于模拟的场景
}
private void OnEnable()
{
LeanTouch.OnFingerDown += OnFingerDown;
LeanTouch.OnFingerSet += OnFingerSet;
LeanTouch.OnFingerUp += OnFingerUp;
}
private void OnDisable()
{
LeanTouch.OnFingerDown -= OnFingerDown;
LeanTouch.OnFingerSet -= OnFingerSet;
LeanTouch.OnFingerUp -= OnFingerUp;
}
//创建反射小球
private void CreateBallReflect()
{
//创建轨迹球
trackBallTransform = ObjectPool.Instance.Spawn("BallTrack", LobbyCtrl.Instance.transform.parent).transform;
trackBallTransform.name = "TrackBall";
trackBallTransform.position = vecBallPoint;
trackBallRigidbody = trackBallTransform.GetComponent<Rigidbody2D>();
UIHelper.SetActive(false, trackBallTransform);
//创建20个白色反射小球,并隐藏
for (int i = 0; i < 20; i++)
{
GameObject ball = ObjectPool.Instance.Spawn(Const.Prefab_Obj_BallReflect, transform);
ball.name = "BallReflect";
GoBallList.Add(ball);
}
for (int i = 0; i < GoBallList.Count; i++)
{
UIHelper.SetActive(false, GoBallList[i]);
}
}
//创建一个用于模拟的场景
private void CreatePhysicsScene()
{
var scene = SceneManager.CreateScene("Physics");
physicsScene = scene.GetPhysicsScene2D();
//将轨迹球移动到物理场景里
SceneManager.MoveGameObjectToScene(trackBallTransform.gameObject, scene);
}
private void MoveFinger(LeanFinger finger)
{
//显示轨迹球
UIHelper.SetActive(true, trackBallTransform);
//关闭 物理
Physics2D.autoSimulation = false;
//每次移动需要重置轨迹球的位置和力
trackBallTransform.position = ballCtrl.transform.position;
trackBallRigidbody.velocity = Vector3.zero;
Vector2 screenPoint = Camera.main.ScreenToWorldPoint(finger.ScreenPosition);
var dir = (screenPoint - (Vector2)ballCtrl.transform.position).normalized;
//让轨迹球开始物理模拟
trackBallRigidbody.AddForce(dir * Const.BallSpeed);
for (int i = 0; i < GoBallList.Count; i++)
{
GameObject ball = GoBallList[i];
UIHelper.SetActive(true, ball);
physicsScene.Simulate(0.02f);//必须加这一句,值越小白色反射球间距越小
ball.transform.position = trackBallTransform.position;//所有的白色反射小球会放置到轨迹球经过的位置上
}
}
//按下
private void OnFingerDown(LeanFinger finger)
{
MoveFinger(finger);
}
//移动
private void OnFingerSet(LeanFinger finger)
{
MoveFinger(finger);
}
//松开
private void OnFingerUp(LeanFinger finger)
{
//打开物理
Physics2D.autoSimulation = true;
}
//回收弹球
void ClearBallList()
{
UIHelper.SetActive(false, trackBallTransform);
for (int i = 0; i < GoBallList.Count; i++)
{
ObjectPool.Instance.Unspawn(GoBallList[i]);
}
}
}
边栏推荐
- Object oriented -- encapsulation, inheritance, polymorphism
- Katalon中控件的参数化
- Nbear introduction and use diagram
- Defensive programming skills
- CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
- Aperçu du code source futur - série juc
- [PaddleSeg 源码阅读] PaddleSeg计算Dice
- 深度优先搜索简要讲解(附带基础题)
- MySQL maxscale realizes read-write separation
- Katalon framework tests web (XXI) to obtain element attribute assertions
猜你喜欢

02 ls 命令的具体实现

基于PHP的轻量企业销售管理系统

Evolution of MySQL database architecture

Session learning diary 1

Rhcsa-- day one

用于TCP协议交互的TCPClientDemo

Mitsubishi M70 macro variable reading Mitsubishi M80 public variable acquisition Mitsubishi CNC variable reading acquisition Mitsubishi CNC remote tool compensation Mitsubishi machine tool online tool

Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..

The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO

laravel admin里百度编辑器自定义路径和文件名
随机推荐
渗透实战-SQLServer提权
基于PHP的轻量企业销售管理系统
Management and thesis of job management system based on SSM
Huawei cloud Kunpeng engineer training (Guangxi University)
A review of reverse reinforcement learning at Virginia Tech (VT)
Katalon中控件的参数化
Smart subway | cloud computing injects wisdom into urban subway transportation
深入浅出对话系统——使用Transformer进行文本分类
Sales management system of lightweight enterprises based on PHP
【.NET+MQTT】.NET6 環境下實現MQTT通信,以及服務端、客戶端的雙邊消息訂閱與發布的代碼演示
智慧地铁| 云计算为城市地铁交通注入智慧
[paddleseg source code reading] paddleseg calculates Miou
[.NET + mqtt]. Mise en œuvre de la communication mqtt dans l'environnement net 6 et démonstration de code pour l'abonnement et la publication de messages bilatéraux du serveur et du client
SQL语句加强练习(MySQL8.0为例)
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
“软硬皆施”,助力建成新型云计算数据中心
Redis cluster uses Lua script. Lua script can also be used for different slots
[PaddleSeg 源码阅读] PaddleSeg计算 mIoU
02 ls 命令的具体实现
Reduce function under functools