素材来自:https://github.com/Matthew-J-Spencer/Trajectory-Line-Unity
做游戏的时候难免会遇到需要物理预测的需求 例如。要求显示炮弹发射轨迹,让玩家拥有更好的操作体验 编写代码过程中。我们可以自己写一套,也可以直接使用自带的场景模拟 自己写一套各有千秋,这里只说Unity自带的物理模拟 SceneManager除了用来加载场景,卸载场景,获取场景对象,还可以创建场景 创建时可以顺便指定创建对应的物理模拟
LocalPhysicsMode | 说明 |
---|
None | 该场景不需要物理 | Physics2D | 创建一个具有2D物理的场景 | Physics3D | 创建一个具有3D物理的场景 |
public PhysicsSimulation(LocalPhysicsMode mode, string sceneName = defaultSceneName)
{
scene = SceneManager.CreateScene(sceneName, new CreateSceneParameters(mode));
if (mode == LocalPhysicsMode.Physics2D)
{
physicsScene = new PhysicsScene2D(scene);
}
else if (mode == LocalPhysicsMode.Physics3D)
{
physicsScene = new PhysicsScene3D(scene);
}
}
想要有一个真实的模拟,需要在模拟场景创建相同物理对象,物理模拟场景并不需要显示,可以把该场景内的对象的渲染关闭
public GameObject Create(GameObject gameObject)
{
var go = GameObject.Instantiate(gameObject, gameObject.transform.position, gameObject.transform.rotation);
foreach (var renderer in go.GetComponentsInChildren<Renderer>())
{
renderer.enabled = false;
}
SceneManager.MoveGameObjectToScene(go, scene);
if(!gameObject.isStatic)
{
dict.Add(gameObject.transform, go.transform);
}
return go;
}
并且还需要同步对象的位置,旋转,缩放。一般情况下同步这三个就够了。 但是如果你自己做了一些骚操作。如动态改变碰撞体大小什么的。就要额外同步碰撞体 onChanged就是为了应对你的骚操作的
public void Changed(Transform transform)
{
if (!dict.ContainsKey(transform))
return;
var value = dict[transform];
value.position = transform.position;
value.rotation = transform.rotation;
value.localScale = transform.localScale;
onChanged?.Invoke(transform);
}
场景内已经有所有需要碰撞的对象了。这个时候就可以开始模拟物理
public void Simulate(Action<int> fixedUpdate, int maxPhysicsFrameIterations)
{
for (var i = 0; i < maxPhysicsFrameIterations; i++)
{
physicsScene.Simulate(Time.fixedDeltaTime);
fixedUpdate?.Invoke(i);
}
}
工具类
public class PhysicsSimulation
{
const string defaultSceneName = "PhysicsSimulation";
public Action<Transform> onChanged;
private Scene scene;
private IPhysicsScene physicsScene;
private readonly Dictionary<Transform, Transform> dict = new Dictionary<Transform, Transform>();
public PhysicsSimulation(LocalPhysicsMode mode, string sceneName = defaultSceneName)
{
scene = SceneManager.CreateScene(sceneName, new CreateSceneParameters(mode));
if (mode == LocalPhysicsMode.Physics2D)
{
physicsScene = new PhysicsScene2D(scene);
}
else if (mode == LocalPhysicsMode.Physics3D)
{
physicsScene = new PhysicsScene3D(scene);
}
}
public void AddScene(GameObject gameObject)
{
SceneManager.MoveGameObjectToScene(gameObject, scene);
}
public void Changed(GameObject gameObject)
{
Changed(gameObject.transform);
}
public void Changed(Transform transform)
{
if (!dict.ContainsKey(transform))
return;
var value = dict[transform];
value.position = transform.position;
value.rotation = transform.rotation;
value.localScale = transform.localScale;
onChanged?.Invoke(transform);
}
public Transform Create(Transform transform)
{
return Create(transform.gameObject).transform;
}
public GameObject Create(GameObject gameObject)
{
var go = GameObject.Instantiate(gameObject, gameObject.transform.position, gameObject.transform.rotation);
foreach (var renderer in go.GetComponentsInChildren<Renderer>())
{
renderer.enabled = false;
}
SceneManager.MoveGameObjectToScene(go, scene);
if(!gameObject.isStatic)
{
dict.Add(gameObject.transform, go.transform);
}
return go;
}
public void Remove(Transform transform)
{
if (!dict.ContainsKey(transform))
return;
GameObject.Destroy(dict[transform]);
dict.Remove(transform);
}
public void Simulate(Action<int> fixedUpdate, int maxPhysicsFrameIterations)
{
for (var i = 0; i < maxPhysicsFrameIterations; i++)
{
physicsScene.Simulate(Time.fixedDeltaTime);
fixedUpdate?.Invoke(i);
}
}
}
interface IPhysicsScene
{
void Simulate(float step);
}
class PhysicsScene3D : IPhysicsScene
{
UnityEngine.PhysicsScene physicsScene;
public PhysicsScene3D(Scene scene)
{
this.physicsScene = scene.GetPhysicsScene();
}
void IPhysicsScene.Simulate(float step)
{
physicsScene.Simulate(step);
}
}
class PhysicsScene2D : IPhysicsScene
{
UnityEngine.PhysicsScene2D physicsScene;
public PhysicsScene2D(Scene scene)
{
this.physicsScene = scene.GetPhysicsScene2D();
}
void IPhysicsScene.Simulate(float step)
{
physicsScene.Simulate(step);
}
}
demo
public class Projection : MonoBehaviour
{
[SerializeField] private LineRenderer _line;
[SerializeField] private int _maxPhysicsFrameIterations = 100;
[SerializeField] private Transform _obstaclesParent;
public PhysicsSimulation physicsSimulation;
private void Start()
{
physicsSimulation = new PhysicsSimulation(LocalPhysicsMode.Physics3D);
foreach (Transform obj in _obstaclesParent)
{
physicsSimulation.Create(obj);
}
}
private void Update()
{
foreach (Transform obj in _obstaclesParent)
{
physicsSimulation.Changed(obj);
}
}
public void SimulateTrajectory(Ball ballPrefab, Vector3 pos, Vector3 velocity)
{
var ghostObj = Instantiate(ballPrefab, pos, Quaternion.identity);
physicsSimulation.AddScene(ghostObj.gameObject);
ghostObj.Init(velocity, true);
_line.positionCount = _maxPhysicsFrameIterations;
physicsSimulation.Simulate(index =>
{
_line.SetPosition(index, ghostObj.transform.position);
}, _maxPhysicsFrameIterations);
Destroy(ghostObj.gameObject);
}
}
|