gpt4 book ai didi

c# - 如何正确使用 Physics.autoSimulation?

转载 作者:行者123 更新时间:2023-11-30 16:42:38 25 4
gpt4 key购买 nike

我正在尝试使用 Physics.autoSimulation 预测游戏对象的位置(游戏对象附有刚体)。问题是当我触发模拟时,对象有某种闪烁。像这样:

enter image description here

我读了这个thread .我开始认为我没有正确使用 autoSimulation。这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestBall : MonoBehaviour {


public float timeCheck = 3f;
public float force = 10f;

private Rigidbody rb;

void Start () {

rb = GetComponent<Rigidbody>();

}//end Start



void Update () {

if (Input.GetKeyDown(KeyCode.Space)) {

//Starts the simulation...
Physics.autoSimulation = false;
rb.AddForce(Vector3.right * force, ForceMode.Impulse);
Vector3 futurePosition = CheckPosition(rb);
print("Future position at " + futurePosition);

//...and move the GameObject for real
rb.AddForce(Vector3.right * force, ForceMode.Impulse);

}//end if

}//end Update



Vector3 CheckPosition (Rigidbody defaultRb) {

Vector3 defaultPos = defaultRb.position;
Quaternion defaultRot = defaultRb.rotation;

float timeInSec = timeCheck;

while (timeInSec >= Time.fixedDeltaTime) {

timeInSec -= Time.fixedDeltaTime;
Physics.Simulate(Time.fixedDeltaTime);

}//end while

Vector3 futurePos = defaultRb.position;

Physics.autoSimulation = true;

defaultRb.velocity = Vector3.zero;
defaultRb.position = defaultPos;
defaultRb.rotation = defaultRot;

return futurePos;

}//end CheckPosition

}//end TestBall

就好像有那么一小会儿,游戏对象在模拟结束时移动了,然后又回来了,因为位置在 CheckPosition 函数中重置了。

感谢您的帮助!

最佳答案

您正在正确使用 Physics.autoSimulation。问题在于您如何重置位置。

修改刚体的位置和旋转不同于修改其变换的位置和旋转。您想要使用变换而不是刚体来重置对象的变换,这样移动将立即发生而不是在多个帧上发生,因为这将使它在屏幕上可见。

替换:

defaultRb.position = defaultPos;
defaultRb.rotation = defaultRot;

defaultRb.transform.position = defaultPos;
defaultRb.transform.rotation = defaultRot;

此外,由于这是一个滚球,设置 velocity 不足以阻止它。您还必须设置它的 angularVelocity,因为球在旋转和平移:

defaultRb.velocity = Vector3.zero;
defaultRb.angularVelocity = Vector3.zero;

这是没有闪烁的新 CheckPosition 函数:

Vector3 CheckPosition(Rigidbody defaultRb)
{

Vector3 defaultPos = defaultRb.position;
Quaternion defaultRot = defaultRb.rotation;

float timeInSec = timeCheck;

while (timeInSec >= Time.fixedDeltaTime)
{

timeInSec -= Time.fixedDeltaTime;
Physics.Simulate(Time.fixedDeltaTime);

}//end while

Vector3 futurePos = defaultRb.position;


Physics.autoSimulation = true;

defaultRb.velocity = Vector3.zero;
defaultRb.angularVelocity = Vector3.zero;

defaultRb.transform.position = defaultPos;
defaultRb.transform.rotation = defaultRot;

return futurePos;

}//end CheckPosition

最后,您当前的代码不会在旧的强制之上添加强制。这意味着如果您按下空格键并再次按下,则不会在旧力之上添加力。相反,对象停止,然后添加新的力。旧的丢了。

如果您想在旧力之上添加新力,您必须在模拟前存储当前速度和角速度,然后在模拟后将它们应用回刚体。 物体速度应随时间增加。

这与您的问题无关,只是代码中的另一个错误。下面是对此的修复(新的 UpdateCheckPosition 函数):

void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//Get current velocity and angularVelocity
Vector3 defaultVelocity = rb.velocity;
Vector3 defaultAngularVelocity = rb.angularVelocity;

//Starts the simulation...
Physics.autoSimulation = false;
rb.AddForce(Vector3.right * force, ForceMode.Impulse);
Vector3 futurePosition = CheckPosition(rb, defaultVelocity, defaultAngularVelocity);
print("Future position at " + futurePosition);

//...and move the GameObject for real
rb.AddForce(Vector3.right * force, ForceMode.Impulse);

}//end if

}//end Update



Vector3 CheckPosition(Rigidbody defaultRb, Vector3 defaultVelocity, Vector3 defaultAngularVelocity)
{

Vector3 defaultPos = defaultRb.position;
Quaternion defaultRot = defaultRb.rotation;

float timeInSec = timeCheck;

while (timeInSec >= Time.fixedDeltaTime)
{
timeInSec -= Time.fixedDeltaTime;
Physics.Simulate(Time.fixedDeltaTime);

}//end while

Vector3 futurePos = defaultRb.position;


Physics.autoSimulation = true;

//Stop object force
defaultRb.velocity = Vector3.zero;
defaultRb.angularVelocity = Vector3.zero;

//Reset Position
defaultRb.transform.position = defaultPos;
defaultRb.transform.rotation = defaultRot;

//Apply old velocity and angularVelocity
defaultRb.velocity = defaultVelocity;
defaultRb.angularVelocity = defaultAngularVelocity;

return futurePos;

}//end CheckPosition

关于c# - 如何正确使用 Physics.autoSimulation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46476204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com