当前位置:网站首页>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]);
}
}
}
边栏推荐
- Database SQL statement summary, continuous update
- 思考的小记录
- Future源碼一觀-JUC系列
- Value transfer communication between components (parent to child, child to parent, brother component to value)
- warning: LF will be replaced by CRLF in XXXXXX
- [PaddleSeg 源码阅读] PaddleSeg计算Dice
- 毕业总结
- 【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
- Zigzag scan
- Support the first triggered go ticker
猜你喜欢
Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
The maximum expiration time of client secret in azure ad application registration is modified to 2 years
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
基于PHP的轻量企业销售管理系统
如何有效远程办公之我见 | 社区征文
Reduce function under functools
Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
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.
Tcpclientdemo for TCP protocol interaction
随机推荐
Msgraphmailbag - search only driveitems of file types
Epidemic strikes -- Thinking about telecommuting | community essay solicitation
Objective C attribute keyword
Redis cluster view the slots of each node
[paddleseg source code reading] paddleseg custom data class
1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
postgresql 用户不能自己创建表格配置
Third party login initial version
[book club issue 13] multimedia processing tool ffmpeg tool set
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
[.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
深度优先搜索简要讲解(附带基础题)
Calculate the odd sum of 1~n (1~100 as an example)
functools下的reduce函数
JVM family -- heap analysis
'2'&gt;' 10'==true? How does JS perform implicit type conversion?
National standard gb28181 protocol platform easygbs fails to start after replacing MySQL database. How to deal with it?
[book club issue 13] packaging format of video files
Getting started with the go language is simple: go implements the Caesar password