当前位置:网站首页>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]);
}
}
}
边栏推荐
- Aperçu du code source futur - série juc
- Msgraphmailbag - search only driveitems of file types
- 思考的小记录
- SDP中的SPA
- PostgreSQL users cannot create table configurations by themselves
- Support the first triggered go ticker
- 渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
- Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
- Katalon中控件的参数化
- Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
猜你喜欢
SQL statement strengthening exercise (MySQL 8.0 as an example)
Why is it recommended that technologists write blogs?
函数计算异步任务能力介绍 - 任务触发去重
MySQL one master multiple slaves + linear replication
Getting started with the go language is simple: go implements the Caesar password
Katalon中控件的参数化
National standard gb28181 protocol platform easygbs fails to start after replacing MySQL database. How to deal with it?
Objective-C description method and type method
三年进账35.31亿,这个江西老表要IPO了
Brief explanation of depth first search (with basic questions)
随机推荐
Es network layer
三年进账35.31亿,这个江西老表要IPO了
Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
Brief explanation of depth first search (with basic questions)
Redis cluster uses Lua script. Lua script can also be used for different slots
Database SQL statement summary, continuous update
Objective-C string class, array class
Idea configuration 360zip open by default -- external tools
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
ctf-pikachu-CSRF
思考的小记录
"Implement both software and hardware" to help build a new cloud computing data center
Mindmanager2022 efficient and easy to use office mind map MindManager
华为云鲲鹏工程师培训(广西大学)
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
02 ls 命令的具体实现
SDP中的SPA
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!
vue多级路由嵌套怎么动态缓存组件