当前位置:网站首页>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]);
}
}
}
边栏推荐
- 01 qemu 启动编译好的镜像 VFS: Unable to mount root fs on unknown-block(0,0)
- SQL語句加强練習(MySQL8.0為例)
- Smart subway | cloud computing injects wisdom into urban subway transportation
- Getting started with the go language is simple: go implements the Caesar password
- STM32外接DHT11显示温湿度
- CSP drawing
- Es network layer
- [paddleseg source code reading] paddleseg calculation dice
- 【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
- Which product is better if you want to go abroad to insure Xinguan?
猜你喜欢

SQL statement strengthening exercise (MySQL 8.0 as an example)

Select sorting and bubble sorting template

Brief explanation of depth first search (with basic questions)

What kind of experience is it when the Institute earns 20000 yuan a month!

Typical applications of minimum spanning tree

ctf-pikachu-CSRF

02 ls 命令的具体实现

Idea configuration 360zip open by default -- external tools

JSON string conversion in unity

SQL语句加强练习(MySQL8.0为例)
随机推荐
MySQL maxscale realizes read-write separation
Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
CUDA basic knowledge
渗透实战-SQLServer提权
Future源碼一觀-JUC系列
【华为云IoT】读书笔记之《万物互联:物联网核心技术与安全》第3章(上)
Redis cluster uses Lua script. Lua script can also be used for different slots
'2'&gt;' 10'==true? How does JS perform implicit type conversion?
"Implement both software and hardware" to help build a new cloud computing data center
[.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
[PaddleSeg 源码阅读] PaddleSeg计算 mIoU
Pytest multi process / multi thread execution test case
LevelDB源码解读-SkipList
[book club issue 13] packaging format of video files
三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量
【CSRF-01】跨站请求伪造漏洞基础原理及攻防
2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
ctf-pikachu-CSRF
My opinion on how to effectively telecommute | community essay solicitation
Redis cluster view the slots of each node