当前位置:网站首页>Unity draws the trajectory of pinball and billiards
Unity draws the trajectory of pinball and billiards
2022-07-04 04:18:00 【Cuijiahao】
design sketch
I don't say much nonsense , Go straight to the renderings

principle
The principle is to create a trackball , In the second scene, simulate the physical trajectory of the white ball , And record the track ! That is to create an invisible scene , Used to simulate the trajectory drawing of the ball .
After the collision , Draw the motion trajectory of the impacted object Transfer momentum to the collision object , Get the object of collision . So as to record the motion trajectory of the collided object
scene
Physics For physical scenes ,GoBallList Used to store the small ball to be reflected ,TrackBallTransform For the trackball to be simulated

Code
There are two key codes :
call physicsScene.Simulate(0.02f); Complete trajectory prediction ;
This API need Physics.autoSimulation = false;// close physical simulation
The specific code is as follows :
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>();// All the reflection balls
public Transform trackBallTransform;// trackball ( That is, a small ball that needs physical simulation )
private Rigidbody2D trackBallRigidbody;// The rigid body of the trackball
private PhysicsScene2D physicsScene; // Physical scenes
public Vector3 vecBallPoint;// The position of the trackball
private void Start()
{
CreateBallReflect();// Create a reflection ball
CreatePhysicsScene();// Create a scene for simulation
}
private void OnEnable()
{
LeanTouch.OnFingerDown += OnFingerDown;
LeanTouch.OnFingerSet += OnFingerSet;
LeanTouch.OnFingerUp += OnFingerUp;
}
private void OnDisable()
{
LeanTouch.OnFingerDown -= OnFingerDown;
LeanTouch.OnFingerSet -= OnFingerSet;
LeanTouch.OnFingerUp -= OnFingerUp;
}
// Create a reflection ball
private void CreateBallReflect()
{
// Create trackball
trackBallTransform = ObjectPool.Instance.Spawn("BallTrack", LobbyCtrl.Instance.transform.parent).transform;
trackBallTransform.name = "TrackBall";
trackBallTransform.position = vecBallPoint;
trackBallRigidbody = trackBallTransform.GetComponent<Rigidbody2D>();
UIHelper.SetActive(false, trackBallTransform);
// establish 20 A white reflection ball , And hide
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]);
}
}
// Create a scene for simulation
private void CreatePhysicsScene()
{
var scene = SceneManager.CreateScene("Physics");
physicsScene = scene.GetPhysicsScene2D();
// Move the trackball into the physical scene
SceneManager.MoveGameObjectToScene(trackBallTransform.gameObject, scene);
}
private void MoveFinger(LeanFinger finger)
{
// Show trackball
UIHelper.SetActive(true, trackBallTransform);
// close Physics
Physics2D.autoSimulation = false;
// Each move requires resetting the position and force of the trackball
trackBallTransform.position = ballCtrl.transform.position;
trackBallRigidbody.velocity = Vector3.zero;
Vector2 screenPoint = Camera.main.ScreenToWorldPoint(finger.ScreenPosition);
var dir = (screenPoint - (Vector2)ballCtrl.transform.position).normalized;
// Let the trackball start the physical simulation
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);// It must be added that , The smaller the value, the smaller the spacing of white reflection balls
ball.transform.position = trackBallTransform.position;// All white reflection balls will be placed where the trackball passes
}
}
// Press down
private void OnFingerDown(LeanFinger finger)
{
MoveFinger(finger);
}
// Move
private void OnFingerSet(LeanFinger finger)
{
MoveFinger(finger);
}
// Release
private void OnFingerUp(LeanFinger finger)
{
// Open Physics
Physics2D.autoSimulation = true;
}
// Recycle Pinball
void ClearBallList()
{
UIHelper.SetActive(false, trackBallTransform);
for (int i = 0; i < GoBallList.Count; i++)
{
ObjectPool.Instance.Unspawn(GoBallList[i]);
}
}
}
边栏推荐
- 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.
- Sales management system of lightweight enterprises based on PHP
- My opinion on how to effectively telecommute | community essay solicitation
- 1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
- hbuildx中夜神模拟器的配置以及热更新
- 图解网络:什么是热备份路由器协议HSRP?
- Exercises in quantum mechanics
- Graduation summary
- Graduation project: design seckill e-commerce system
- laravel admin里百度编辑器自定义路径和文件名
猜你喜欢

Rhcsa-- day one

Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
![[Logitech] m720](/img/bb/44144a1c3907808398c05b3b36962c.png)
[Logitech] m720

leetcode刷题:二叉树05(翻转二叉树)

Confession code collection, who says program apes don't understand romance

用于TCP协议交互的TCPClientDemo

Perf simple process for multithreaded profile

PostgreSQL users cannot create table configurations by themselves

Exercises in quantum mechanics

Illustrated network: what is the hot backup router protocol HSRP?
随机推荐
JDBC 进阶
Katalon uses script to query list size
Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
Getting started with the go language is simple: go implements the Caesar password
Reduce function under functools
VIM mapping command
线程常用的方法
PPt 教程,如何在 PowerPoint 中将演示文稿另存为 PDF 文件?
How was my life in 2021
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
Activiti7 task service - process variables (setvariable and setvariablelocal)
函数计算异步任务能力介绍 - 任务触发去重
1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
Flink learning 7: application structure
Understand the principle of bytecode enhancement technology through the jvm-sandbox source code
VIM add interval annotation correctly
深度优先搜索简要讲解(附带基础题)
[paddleseg source code reading] paddleseg calculates Miou
疫情来袭--远程办公之思考|社区征文
Illustrated network: what is the hot backup router protocol HSRP?